diff --git a/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.spec.ts b/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.spec.ts index 1d3a06aa4f424ac78ca5898dbe7b55354ccdeba2..d6ff6dfa2d7082c4e314dcd7a7d05bb919aeb8fa 100644 --- a/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.spec.ts +++ b/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.spec.ts @@ -1,6 +1,6 @@ import { ADMIN_FORMSERVICE } from '@admin-client/shared'; import { NavigationService } from '@alfa-client/navigation-shared'; -import { AbstractFormService, createStateResource, isValidStateResource, StateResource } from '@alfa-client/tech-shared'; +import { AbstractFormService, createStateResource, isLoaded, StateResource } from '@alfa-client/tech-shared'; import { dispatchEventFromFixture, getMockComponent, mock, Mock, MockEvent } from '@alfa-client/test-utils'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { faker } from '@faker-js/faker/.'; @@ -17,10 +17,10 @@ import { AdminSaveButtonComponent } from './admin-save-button.component'; jest.mock('@alfa-client/tech-shared', () => { return { ...jest.requireActual('@alfa-client/tech-shared'), - isValidStateResource: jest.fn(), + isLoaded: jest.fn(), }; }); -const isValidStateResourceMock: jest.Mock = isValidStateResource as jest.Mock; +const isLoadedMock: jest.Mock = isLoaded as jest.Mock; describe('AdminSaveButtonComponent', () => { let component: AdminSaveButtonComponent; @@ -61,36 +61,50 @@ describe('AdminSaveButtonComponent', () => { expect(component).toBeTruthy(); }); - describe('on submit', () => { - it('should call formService', () => { - dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); + describe('component', () => { + describe('on successful form submission', () => { + it('should navigate to success link path', () => { + const successLinkPath: string = faker.internet.url(); + component.successLinkPath = successLinkPath; + isLoadedMock.mockReturnValue(true); - expect(formService.submit).toHaveBeenCalled(); - }); + component._navigateOnSuccessfulSubmission(createStateResource(createDummyResource())); + + expect(navigationService.navigate).toHaveBeenCalledWith(successLinkPath); + }); + + it('should NOT navigate', () => { + isLoadedMock.mockReturnValue(false); - it('should assign state resource', () => { - dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); + component._navigateOnSuccessfulSubmission(createStateResource(createDummyResource())); - expect(component.stateResource$).toBeObservable(singleColdCompleted(stateResource)); + expect(navigationService.navigate).not.toHaveBeenCalled(); + }); }); - it('should navigate on successful submit', () => { - isValidStateResourceMock.mockReturnValue(true); - const linkPath: string = faker.internet.url(); - component.successLinkPath = linkPath; + describe('on submit', () => { + beforeEach(() => { + component._navigateOnSuccessfulSubmission = jest.fn(); + }); - dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); - component.stateResource$.subscribe(); + it('should call formService', () => { + dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); - expect(navigationService.navigate).toHaveBeenCalledWith(linkPath); - }); + expect(formService.submit).toHaveBeenCalled(); + }); + + it('should assign state resource', () => { + dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); + + expect(component.stateResource$).toBeObservable(singleColdCompleted(stateResource)); + }); - it('should NOT navigate on invalid state resource', () => { - isValidStateResourceMock.mockReturnValue(false); - dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); - component.stateResource$.subscribe(); + it('should navigate on successful submit', () => { + dispatchEventFromFixture(fixture, saveButton, MockEvent.CLICK); + component.stateResource$.subscribe(); - expect(navigationService.navigate).not.toHaveBeenCalled(); + expect(component._navigateOnSuccessfulSubmission).toHaveBeenCalledWith(stateResource); + }); }); }); diff --git a/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.ts b/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.ts index 7f0ff88dcdf4fe78991bff1390a17e1f17da1bef..598d056ea60aeb0edc17497fd923982ed4533474 100644 --- a/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.ts +++ b/alfa-client/libs/admin/shared/src/lib/admin-save-button/admin-save-button.component.ts @@ -1,6 +1,6 @@ import { ADMIN_FORMSERVICE } from '@admin-client/shared'; import { NavigationService } from '@alfa-client/navigation-shared'; -import { AbstractFormService, createEmptyStateResource, isValidStateResource, StateResource } from '@alfa-client/tech-shared'; +import { AbstractFormService, createEmptyStateResource, isLoaded, StateResource } from '@alfa-client/tech-shared'; import { CommonModule } from '@angular/common'; import { Component, inject, Input } from '@angular/core'; import { Resource } from '@ngxp/rest'; @@ -24,10 +24,14 @@ export class AdminSaveButtonComponent { public submit(): void { this.stateResource$ = this.formService.submit().pipe( tap((stateResource: StateResource<Resource>) => { - if (isValidStateResource(stateResource)) { - this.navigationService.navigate(this.successLinkPath); - } + this._navigateOnSuccessfulSubmission(stateResource); }), ); } + + _navigateOnSuccessfulSubmission(stateResource: StateResource<Resource>) { + if (isLoaded(stateResource)) { + this.navigationService.navigate(this.successLinkPath); + } + } }