I created service which is sending data to back-end, data was filled by user in UI. User can also upload any file to send it to back-end too. I am trying to test this functionality with jasmine marbles.
Here is my service code:
export class FormSubmitService {
constructor(private http: HttpClient) {}
public submit(data, attachments) {
const observables = [
...attachments.map((file) => this.uploadAttachment(file)),
];
return forkJoin(...observables)
.pipe(
defaultIfEmpty([]),
map((tokens) => {
return tokens.map((tokenObj) => tokenObj.attachment_token);
}),
switchMap((tokens: string[]) => {
const formData = {
data,
attachments: tokens,
};
return this.submitForm(formData);
}),
);
}
private uploadAttachment(attachment: File) {
const attachmentData: FormData = new FormData();
attachmentData.append('attachment', attachment, attachment.name);
return this.http.post(
'/some/form/endpoint/attachments',
attachmentData,
);
}
private submitForm(userData: UserData) {
return this.http.post(
`/some/form/endpoint/form`,
userData,
);
}
}
If user adds one or more attachments, then before I send user data to back-end, I need to upload each attachment to back-end to get each attachment's token which later I am storing in array. I am doing it with forkJoin()
, waiting till all attachments are uploaded and then using switchMap
to submitting user data.
Here are two of my test cases (one working, one not working):
describe('SubmitFormData', () => {
let service: FormSubmitService;
let http: jasmine.SpyObj<HttpClient>;
beforeEach(() => {
http = jasmine.createSpyObj('HttpClient', ['post']);
service = new FormSubmitService(http);
});
describe('#submit', () => {
const file = new File([''], 'filename', { type: 'image/png' });
const attachments = [file];
const data = {
name: 'name',
description: 'description',
};
// NOT WORKING!
it('submit with attachment', () => {
const expected = cold('-a--b-', { a: ['token'], b: { id: 'id_123' } }); // FAIL!
// const expected = cold('----'); // SUCCESS!
http.post.and.returnValues(
cold('-a-', { a: [{ attachment_token: 'token' }] }),
cold('-a-', { a: { id: 'id_123' } }),
);
const output = service.submit(data, attachments);
expect(output).toBeObservable(expected);
expect(http.post).toHaveBeenCalled();
});
// WORKING!
it('submit without attachment', () => {
const response = {
id: 'id_123',
};
const expected = cold('-a-', { a: response });
http.post.and.returnValues(
cold('-a-', { a: { id: 'id_123' } }),
);
const output = service.submit(data, []);
expect(output).toBeObservable(expected);
expect(http.post).toHaveBeenCalled();
});
});
});
Test where form data is without attachments is successful, but test where form data is with attachment fails.
Error message from failure:
✖ submit with attachment HeadlessChrome 71.0.3578 (Mac OS X 10.14.2) Error: Expected $.length = 0 to equal 2. Expected $[0] = undefined to equal Object({ frame: 10, notification: Notification({ kind: 'N', value: [ 'token' ], error: undefined, hasValue: true }) }). Expected $[1] = undefined to equal Object({ frame: 40, notification: Notification({ kind: 'N', value: Object({ id: 'id_123' }), error: undefined, hasValue: true }) }).
Seems like output
is not emitting observable in failed test and is undefined
, but question is - why?
Because in another test it emitting it when not sending attachment and using forkJoin()
.
Anyone have idea why it could be like that? Thanks!
Fixed this issue, problem was with first observable which was returned from http.post
call - cold('-a-', { a: [{ attachment_token: 'token' }] }),
. It wasn't emitting new observable and in this point all test stopped. Changed it to of({ attachment_token: 'token' }),
and test is successful.
Here is a code:
it('submit with attachment', () => {
const response = {
id: 'id_123',
};
http.post.and.returnValues(
of({ attachment_token: 'token' }),
cold('-a', { a: response }),
);
const expected = cold('-a', { a: response });
const output = service.submit(data, attachments);
expect(output).toBeObservable(expected);
expect(http.post).toHaveBeenCalledTimes(2);
})