Commit 37d20030 authored by Enrique Alcantara's avatar Enrique Alcantara

Remove waitForPromises in attachment_spec file

parent 509e4ffb
import axios from 'axios'; import axios from 'axios';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import { once } from 'lodash';
import waitForPromises from 'helpers/wait_for_promises';
import Attachment from '~/content_editor/extensions/attachment'; import Attachment from '~/content_editor/extensions/attachment';
import Image from '~/content_editor/extensions/image'; import Image from '~/content_editor/extensions/image';
import Link from '~/content_editor/extensions/link'; import Link from '~/content_editor/extensions/link';
...@@ -12,7 +10,6 @@ import { createTestEditor, createDocBuilder } from '../test_utils'; ...@@ -12,7 +10,6 @@ import { createTestEditor, createDocBuilder } from '../test_utils';
describe('content_editor/extensions/attachment', () => { describe('content_editor/extensions/attachment', () => {
let tiptapEditor; let tiptapEditor;
let eq;
let doc; let doc;
let p; let p;
let image; let image;
...@@ -25,6 +22,24 @@ describe('content_editor/extensions/attachment', () => { ...@@ -25,6 +22,24 @@ describe('content_editor/extensions/attachment', () => {
const imageFile = new File(['foo'], 'test-file.png', { type: 'image/png' }); const imageFile = new File(['foo'], 'test-file.png', { type: 'image/png' });
const attachmentFile = new File(['foo'], 'test-file.zip', { type: 'application/zip' }); const attachmentFile = new File(['foo'], 'test-file.zip', { type: 'application/zip' });
const expectDocumentAfterTransaction = ({ number, expectedDoc, action }) => {
return new Promise((resolve) => {
let counter = 1;
const handleTransaction = () => {
if (counter === number) {
expect(tiptapEditor.state.doc.toJSON()).toEqual(expectedDoc.toJSON());
tiptapEditor.off('update', handleTransaction);
resolve();
}
counter += 1;
};
tiptapEditor.on('update', handleTransaction);
action();
});
};
beforeEach(() => { beforeEach(() => {
renderMarkdown = jest.fn(); renderMarkdown = jest.fn();
...@@ -34,7 +49,6 @@ describe('content_editor/extensions/attachment', () => { ...@@ -34,7 +49,6 @@ describe('content_editor/extensions/attachment', () => {
({ ({
builders: { doc, p, image, loading, link }, builders: { doc, p, image, loading, link },
eq,
} = createDocBuilder({ } = createDocBuilder({
tiptapEditor, tiptapEditor,
names: { names: {
...@@ -90,18 +104,14 @@ describe('content_editor/extensions/attachment', () => { ...@@ -90,18 +104,14 @@ describe('content_editor/extensions/attachment', () => {
mock.onPost().reply(httpStatus.OK, successResponse); mock.onPost().reply(httpStatus.OK, successResponse);
}); });
it('inserts an image with src set to the encoded image file and uploading true', (done) => { it('inserts an image with src set to the encoded image file and uploading true', async () => {
const expectedDoc = doc(p(image({ uploading: true, src: base64EncodedFile }))); const expectedDoc = doc(p(image({ uploading: true, src: base64EncodedFile })));
tiptapEditor.on( await expectDocumentAfterTransaction({
'update', number: 1,
once(() => { expectedDoc,
expect(eq(tiptapEditor.state.doc, expectedDoc)).toBe(true); action: () => tiptapEditor.commands.uploadAttachment({ file: imageFile }),
done(); });
}),
);
tiptapEditor.commands.uploadAttachment({ file: imageFile });
}); });
it('updates the inserted image with canonicalSrc when upload is successful', async () => { it('updates the inserted image with canonicalSrc when upload is successful', async () => {
...@@ -116,11 +126,11 @@ describe('content_editor/extensions/attachment', () => { ...@@ -116,11 +126,11 @@ describe('content_editor/extensions/attachment', () => {
), ),
); );
tiptapEditor.commands.uploadAttachment({ file: imageFile }); await expectDocumentAfterTransaction({
number: 2,
await waitForPromises(); expectedDoc,
action: () => tiptapEditor.commands.uploadAttachment({ file: imageFile }),
expect(eq(tiptapEditor.state.doc, expectedDoc)).toBe(true); });
}); });
}); });
...@@ -129,14 +139,14 @@ describe('content_editor/extensions/attachment', () => { ...@@ -129,14 +139,14 @@ describe('content_editor/extensions/attachment', () => {
mock.onPost().reply(httpStatus.INTERNAL_SERVER_ERROR); mock.onPost().reply(httpStatus.INTERNAL_SERVER_ERROR);
}); });
it('resets the doc to orginal state', async () => { it('resets the doc to original state', async () => {
const expectedDoc = doc(p('')); const expectedDoc = doc(p(''));
tiptapEditor.commands.uploadAttachment({ file: imageFile }); await expectDocumentAfterTransaction({
number: 2,
await waitForPromises(); expectedDoc,
action: () => tiptapEditor.commands.uploadAttachment({ file: imageFile }),
expect(eq(tiptapEditor.state.doc, expectedDoc)).toBe(true); });
}); });
it('emits an error event that includes an error message', (done) => { it('emits an error event that includes an error message', (done) => {
...@@ -168,18 +178,14 @@ describe('content_editor/extensions/attachment', () => { ...@@ -168,18 +178,14 @@ describe('content_editor/extensions/attachment', () => {
mock.onPost().reply(httpStatus.OK, successResponse); mock.onPost().reply(httpStatus.OK, successResponse);
}); });
it('inserts a loading mark', (done) => { it('inserts a loading mark', async () => {
const expectedDoc = doc(p(loading({ label: 'test-file' }))); const expectedDoc = doc(p(loading({ label: 'test-file' })));
tiptapEditor.on( await expectDocumentAfterTransaction({
'update', number: 1,
once(() => { expectedDoc,
expect(eq(tiptapEditor.state.doc, expectedDoc)).toBe(true); action: () => tiptapEditor.commands.uploadAttachment({ file: attachmentFile }),
done(); });
}),
);
tiptapEditor.commands.uploadAttachment({ file: attachmentFile });
}); });
it('updates the loading mark with a link with canonicalSrc and href attrs', async () => { it('updates the loading mark with a link with canonicalSrc and href attrs', async () => {
...@@ -196,11 +202,11 @@ describe('content_editor/extensions/attachment', () => { ...@@ -196,11 +202,11 @@ describe('content_editor/extensions/attachment', () => {
), ),
); );
tiptapEditor.commands.uploadAttachment({ file: attachmentFile }); await expectDocumentAfterTransaction({
number: 2,
await waitForPromises(); expectedDoc,
action: () => tiptapEditor.commands.uploadAttachment({ file: attachmentFile }),
expect(eq(tiptapEditor.state.doc, expectedDoc)).toBe(true); });
}); });
}); });
...@@ -212,11 +218,11 @@ describe('content_editor/extensions/attachment', () => { ...@@ -212,11 +218,11 @@ describe('content_editor/extensions/attachment', () => {
it('resets the doc to orginal state', async () => { it('resets the doc to orginal state', async () => {
const expectedDoc = doc(p('')); const expectedDoc = doc(p(''));
tiptapEditor.commands.uploadAttachment({ file: attachmentFile }); await expectDocumentAfterTransaction({
number: 2,
await waitForPromises(); expectedDoc,
action: () => tiptapEditor.commands.uploadAttachment({ file: attachmentFile }),
expect(eq(tiptapEditor.state.doc, expectedDoc)).toBe(true); });
}); });
it('emits an error event that includes an error message', (done) => { it('emits an error event that includes an error message', (done) => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment