Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed May 27, 2024
1 parent d0015e1 commit a860c00
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('UsersService', () => {
users = result;
});

const req = httpMock.expectOne('http://service/p/api/user-management?take=20&skip=30&query=');
const req = httpMock.expectOne('http://service/p/api/user-management?take=20&skip=30');

expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-Match')).toBeNull();
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/app/framework/angular/http/http-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ export module HTTP {
function getFormData(file: UploadFile) {
const formData = new FormData();

if (file instanceof File) {
formData.append('file', file);
const untyped = file as any;
if (Types.isObject(untyped) && Types.isString(untyped['name']) && Types.isString(untyped['url'])) {
formData.append('url', untyped.url);
formData.append('name', untyped.name);
} else {
formData.append('url', file.url);
formData.append('name', file.name);
formData.append('file', untyped);
}

return formData;
Expand Down
30 changes: 24 additions & 6 deletions frontend/src/app/framework/utils/string-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,33 @@ describe('StringHelper', () => {
expect(StringHelper.hashCode('ABC')).not.toBe(StringHelper.hashCode('XYZ'));
});

it('should append query string to url if url already contains query', () => {
const url = StringHelper.appendToUrl('http://squidex.io?query=value', 'other', 1);
it('should build query for empty object', () => {
const url = StringHelper.buildQuery({});

expect(url).toEqual('http://squidex.io?query=value&other=1');
expect(url).toEqual('');
});

it('should append query string to url if url already contains no query', () => {
const url = StringHelper.appendToUrl('http://squidex.io', 'other', 1);
it('should build query for single value', () => {
const url = StringHelper.buildQuery({ key1: '42' });

expect(url).toEqual('http://squidex.io?other=1');
expect(url).toEqual('?key1=42');
});

it('should build query for multiple values', () => {
const url = StringHelper.buildQuery({ key1: '42', key2: 21 });

expect(url).toEqual('?key1=42&key2=21');
});

it('should build query and ignore null and undefined', () => {
const url = StringHelper.buildQuery({ key1: '42', key2: 21, key: undefined, key4: null });

expect(url).toEqual('?key1=42&key2=21');
});

it('should build query with encoded values', () => {
const url = StringHelper.buildQuery({ key1: 'Hello World' });

expect(url).toEqual('?key1=Hello%20World');
});
});
2 changes: 1 addition & 1 deletion frontend/src/app/shared/services/search.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('SearchService', () => {
results = result;
});

const req = httpMock.expectOne('http://service/p/api/apps/my-app/search/?query=my-query');
const req = httpMock.expectOne('http://service/p/api/apps/my-app/search?query=my-query');

expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-Match')).toBeNull();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/shared/services/stock-photo.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('StockPhotoService', () => {
images = result;
});

const req = httpMock.expectOne('https://stockphoto.squidex.io/?query=my-query&page=4');
const req = httpMock.expectOne('https://stockphoto.squidex.io?query=my-query&page=4');

expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-Match')).toBeNull();
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('StockPhotoService', () => {
images = result;
});

const req = httpMock.expectOne('https://stockphoto.squidex.io/?query=my-query&page=1');
const req = httpMock.expectOne('https://stockphoto.squidex.io?query=my-query&page=1');

expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-Match')).toBeNull();
Expand Down
26 changes: 0 additions & 26 deletions frontend/src/app/shared/services/translations.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,4 @@ describe('TranslationsService', () => {

expect(translation!).toEqual(new TranslationDto('Translated', 'Hallo'));
}));

it('should make post request to ask question',
inject([TranslationsService, HttpTestingController], (translationsService: TranslationsService, httpMock: HttpTestingController) => {
const dto = { prompt: 'My Question' };

let answers: ReadonlyArray<string>;

translationsService.ask('my-app', dto).subscribe(result => {
answers = result;
});

const req = httpMock.expectOne('http://service/p/api/apps/my-app/ask');

expect(req.request.method).toEqual('POST');
expect(req.request.headers.get('If-Match')).toBeNull();

req.flush([
'Answer1',
'Answer2',
]);

expect(answers!).toEqual([
'Answer1',
'Answer2',
]);
}));
});
2 changes: 1 addition & 1 deletion frontend/src/app/shared/services/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('UsersService', () => {
users = result;
});

const req = httpMock.expectOne('http://service/p/api/users?query=');
const req = httpMock.expectOne('http://service/p/api/users');

expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-Match')).toBeNull();
Expand Down

0 comments on commit a860c00

Please sign in to comment.