Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Flesh out AttributeHelpTextModalService spec

Alexander Brandon Coles12f45ce1ccb6
diff --git a/frontend/src/app/shared/components/attribute-help-texts/attribute-help-text-modal.service.spec.ts b/frontend/src/app/shared/components/attribute-help-texts/attribute-help-text-modal.service.spec.ts
index e2fa875d323..845711ae1e5 100644
--- a/frontend/src/app/shared/components/attribute-help-texts/attribute-help-text-modal.service.spec.ts
+++ b/frontend/src/app/shared/components/attribute-help-texts/attribute-help-text-modal.service.spec.ts
@@ -8,7 +8,7 @@ import { ToastService } from '../toaster/toast.service';
describe('AttributeHelpTextModalService', () => {
let fetchSpy:jasmine.Spy<Window['fetch']>;
let modalService:AttributeHelpTextModalService;
- let response:Response;
+ let dialog:HTMLDialogElement|null;
beforeEach(() => {
fetchSpy = spyOn(window, 'fetch');
@@ -32,30 +32,38 @@ describe('AttributeHelpTextModalService', () => {
expect(modalService).toBeTruthy();
});
+ const makeSuccessResponse = (dialogId:string, dialogContent:string) => {
+ const body = `<turbo-stream action="dialog">
+ <template>
+ <dialog-helper>
+ <dialog id="${dialogId}">${dialogContent}</dialog>
+ </dialog-helper>
+ </template>
+ </turbo-stream>`;
+
+ return new Response(body, { status: 200, headers: { 'Content-Type': 'text/vnd.turbo-stream.html' } });
+ };
+
+ afterEach(() => {
+ dialog?.remove();
+ dialog = null;
+ });
+
describe('with a successful request', () => {
beforeEach(() => {
- response = new Response(`
- <turbo-stream action="dialog" target="">
- <template>
- <dialog>Hello Dialog</dialog>
- </template>
- </turbo-stream>`,
- {
- status: 200,
- headers: { 'Content-Type': 'text/vnd.turbo-stream.html' }
- }
- );
- fetchSpy.and.resolveTo(response);
+ fetchSpy
+ .withArgs(jasmine.stringMatching('/1/show_dialog'), jasmine.any(Object))
+ .and.resolveTo(makeSuccessResponse('test1', 'Hello Dialog'));
});
- it('should make a request and handle Turbo Stream dialog response', async () => {
- expect(document.querySelector('dialog')).toBeFalsy();
+ it('should handle Turbo Stream dialog response and open the dialog', async () => {
+ expect(document.querySelector('dialog#test1')).toBeFalsy();
- await modalService.show('1');
+ await expectAsync(modalService.show('1')).toBeResolved();
expect(fetchSpy).toHaveBeenCalledTimes(1);
- const dialog = await waitForNativeElement<HTMLDialogElement>('dialog');
+ dialog = await waitForNativeElement<HTMLDialogElement>('dialog#test1');
expect(dialog.textContent).toEqual('Hello Dialog');
expect(dialog.open).toBeTrue();
@@ -64,6 +72,84 @@ describe('AttributeHelpTextModalService', () => {
expect(dialog.open).toBeFalse();
});
});
+
+ describe('with an aborted request followed by a successful request', () => {
+ beforeEach(() => {
+ fetchSpy
+ .withArgs(jasmine.stringMatching('/2/show_dialog'), jasmine.any(Object))
+ .and.returnValues(
+ Promise.reject(new DOMException('message', 'AbortError')),
+ Promise.resolve(makeSuccessResponse('test2', 'Noch mal ein Dialog')),
+ );
+ });
+
+ it('should handle Turbo Stream dialog response and still open the dialog', async () => {
+ expect(document.querySelector('dialog#test2')).toBeFalsy();
+
+ await expectAsync(modalService.show('2')).toBeRejected();
+ await expectAsync(modalService.show('2')).toBeResolved();
+
+ expect(fetchSpy).toHaveBeenCalledTimes(2);
+
+ dialog = await waitForNativeElement<HTMLDialogElement>('dialog#test2');
+
+ expect(dialog.textContent).toEqual('Noch mal ein Dialog');
+ expect(dialog.open).toBeTrue();
+ dialog.close();
+
+ expect(dialog.open).toBeFalse();
+ });
+ });
+
+ describe('with 3 successful requests with the same dialog id', () => {
+ beforeEach(() => {
+ fetchSpy
+ .withArgs(jasmine.stringMatching('/3/show_dialog'), jasmine.any(Object))
+ .and.returnValues(
+ Promise.resolve(makeSuccessResponse('test3', '<p>initial content</p>')),
+ Promise.resolve(makeSuccessResponse('test3', '<p>updated content</p>')),
+ Promise.resolve(makeSuccessResponse('test3', '<h3>new headline</h3>')),
+ );
+ });
+
+ it('should handle Turbo Stream dialog response and update dialog', async () => {
+ expect(document.querySelector('dialog#test3')).toBeFalsy();
+
+ await expectAsync(modalService.show('3')).toBeResolved();
+
+ expect(fetchSpy).toHaveBeenCalledTimes(1);
+
+ dialog = await waitForNativeElement<HTMLDialogElement>('dialog#test3');
+
+ expect(dialog.textContent).toEqual('initial content');
+ expect(dialog.open).toBeTrue();
+
+ await expectAsync(modalService.show('3')).toBeResolved();
+
+ expect(fetchSpy).toHaveBeenCalledTimes(2);
+
+ let mutation = await waitForElementMutation(dialog);
+
+ expect(mutation.type).toEqual('characterData');
+ expect(dialog.textContent).toEqual('updated content');
+ expect(dialog.open).toBeTrue();
+
+ await expectAsync(modalService.show('3')).toBeResolved();
+
+ expect(fetchSpy).toHaveBeenCalledTimes(3);
+
+ mutation = await waitForElementMutation(dialog);
+
+ expect(mutation.type).toEqual('childList');
+ expect(dialog.textContent).toEqual('new headline');
+ expect(dialog.querySelector('h3')).toBeTruthy();
+ expect(dialog.open).toBeTrue();
+
+ dialog.close();
+
+ expect(dialog.open).toBeFalse();
+ });
+ });
});
function waitForNativeElement<T extends Element>(selector:string):Promise<T> {
@@ -83,3 +169,19 @@ function waitForNativeElement<T extends Element>(selector:string):Promise<T> {
observer.observe(document.body, { childList: true, subtree: true });
});
}
+
+function waitForElementMutation<T extends Element>(
+ element:T,
+ predicate:(mutation:MutationRecord) => boolean = () => true,
+):Promise<MutationRecord> {
+ return new Promise((resolve) => {
+ const observer = new MutationObserver((mutationList) => {
+ const record = mutationList.find(predicate);
+ if (record) {
+ observer.disconnect();
+ resolve(record);
+ }
+ });
+ observer.observe(element, { childList: true, subtree: true, characterData: true });
+ });
+}