diff --git a/package-lock.json b/package-lock.json index 0dd265321..a6c4f11bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -60,7 +60,7 @@ "typeorm": "^0.3.20" }, "devDependencies": { - "@golevelup/ts-jest": "^0.3.3", + "@golevelup/ts-jest": "^0.5.5", "@nestjs/schematics": "^8.0.11", "@nestjs/testing": "^10.4.1", "@types/bull": "^4.10.0", @@ -2211,9 +2211,9 @@ "dev": true }, "node_modules/@golevelup/ts-jest": { - "version": "0.3.3", - "resolved": "https://registry.npmjs.org/@golevelup/ts-jest/-/ts-jest-0.3.3.tgz", - "integrity": "sha512-gut5EhD2S7W1p+C/IsUS1o0P5SHgxsN9TqHyRTjG+rfycniLNBfvIWZPEizpsTev/lR10/XOTpE0YicxPme2+Q==", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/@golevelup/ts-jest/-/ts-jest-0.5.5.tgz", + "integrity": "sha512-x1kAFZ6ADPpwl6rauyJY6uP3OdTXA+BHb2S6nK/dpQcoAyC1gAPlb7kBTFfzvxm8yaoIieaR/638kVZSK6GxtQ==", "dev": true }, "node_modules/@hapi/hoek": { @@ -20358,9 +20358,9 @@ } }, "@golevelup/ts-jest": { - "version": "0.3.3", - "resolved": "https://registry.npmjs.org/@golevelup/ts-jest/-/ts-jest-0.3.3.tgz", - "integrity": "sha512-gut5EhD2S7W1p+C/IsUS1o0P5SHgxsN9TqHyRTjG+rfycniLNBfvIWZPEizpsTev/lR10/XOTpE0YicxPme2+Q==", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/@golevelup/ts-jest/-/ts-jest-0.5.5.tgz", + "integrity": "sha512-x1kAFZ6ADPpwl6rauyJY6uP3OdTXA+BHb2S6nK/dpQcoAyC1gAPlb7kBTFfzvxm8yaoIieaR/638kVZSK6GxtQ==", "dev": true }, "@hapi/hoek": { diff --git a/package.json b/package.json index 742bb0443..9a6dfe3f6 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "body-parser": "^1.20.3" }, "devDependencies": { - "@golevelup/ts-jest": "^0.3.3", + "@golevelup/ts-jest": "^0.5.5", "@nestjs/schematics": "^8.0.11", "@nestjs/testing": "^10.4.1", "@types/bull": "^4.10.0", diff --git a/src/common/slug-generator.spec.ts b/src/common/slug-generator.spec.ts index c57fb8095..6840a103b 100644 --- a/src/common/slug-generator.spec.ts +++ b/src/common/slug-generator.spec.ts @@ -17,7 +17,7 @@ describe('SlugGenerator', () => { describe('when a slug does not exist', () => { it('generates a slug without a number', async () => { - service.findBySlug.mockImplementation(undefined); + service.findBySlug.mockResolvedValue(undefined); const generator = new SlugGenerator(service, 'Some Name'); @@ -51,7 +51,7 @@ describe('SlugGenerator', () => { describe('when a slug does not exist', () => { it('generates a slug without a number', async () => { - service.findBySlug.mockImplementation(undefined); + service.findBySlug.mockResolvedValue(undefined); const generator = new SlugGenerator(service, 'Some Name'); diff --git a/src/decisions/admin/decisions.controller.spec.ts b/src/decisions/admin/decisions.controller.spec.ts index a5c0e54e7..7c9fb2545 100644 --- a/src/decisions/admin/decisions.controller.spec.ts +++ b/src/decisions/admin/decisions.controller.spec.ts @@ -166,8 +166,8 @@ describe('DecisionsController', () => { 2020, 2024, datasets, - i18nService, [], + i18nService, ); expect( DecisionDatasetsPresenter.prototype.present, @@ -270,8 +270,8 @@ describe('DecisionsController', () => { 2020, 2024, datasets, - i18nService, allProfessions, + i18nService, ); expect( DecisionDatasetsPresenter.prototype.present, @@ -320,7 +320,7 @@ describe('DecisionsController', () => { const datasets = decisionDatasetFactory.buildList(3); const allOrganisations = organisationFactory.buildList(3); - decisionDatasetsService.allForOrganisation.mockResolvedValue(datasets); + decisionDatasetsService.all.mockResolvedValue(datasets); organisationVersionsService.allLive.mockResolvedValue(allOrganisations); const filter: FilterDto = { @@ -393,6 +393,9 @@ describe('DecisionsController', () => { decisionDatasetsService.allForOrganisation.mockResolvedValue(datasets); organisationVersionsService.allLive.mockResolvedValue(allOrganisations); + professionVersionsService.allWithLatestVersionForOrganisation.mockResolvedValue( + allProfessions, + ); const filter: FilterDto = { keywords: 'example keywords', diff --git a/src/decisions/admin/new.controller.spec.ts b/src/decisions/admin/new.controller.spec.ts index 44119f6f7..3049817fe 100644 --- a/src/decisions/admin/new.controller.spec.ts +++ b/src/decisions/admin/new.controller.spec.ts @@ -121,6 +121,7 @@ describe('NewController', () => { .mockReturnValue({ start: 2020, end: 2024 }); professionVersionsService.allLive.mockResolvedValue(professionVersions); + organisationVersionsService.allLive.mockResolvedValue(organisations); ( NewDecisionDatasetPresenter.prototype.present as jest.Mock diff --git a/src/decisions/admin/presenters/decision-datasets.presenter.spec.ts b/src/decisions/admin/presenters/decision-datasets.presenter.spec.ts index a15fc7bfe..121844c25 100644 --- a/src/decisions/admin/presenters/decision-datasets.presenter.spec.ts +++ b/src/decisions/admin/presenters/decision-datasets.presenter.spec.ts @@ -150,8 +150,11 @@ describe('DecisionDatasetsPresenter', () => { expect(getUserOrganisationSpy).toHaveBeenCalledWith(user, i18nService); - expect(ListEntryPresenter.headings).toBeCalledWith(false, i18nService); - expect(ListEntryPresenter.prototype.tableRow).toBeCalledTimes(3); + expect(ListEntryPresenter.headings).toHaveBeenCalledWith( + i18nService, + 'single-organisation', + ); + expect(ListEntryPresenter.prototype.tableRow).toHaveBeenCalledTimes(3); expect(OrganisationsCheckboxPresenter).toHaveBeenCalledWith( allOrganisations, @@ -274,8 +277,11 @@ describe('DecisionDatasetsPresenter', () => { expect(getUserOrganisationSpy).toHaveBeenCalledWith(user, i18nService); - expect(ListEntryPresenter.headings).toBeCalledWith(true, i18nService); - expect(ListEntryPresenter.prototype.tableRow).toBeCalledTimes(3); + expect(ListEntryPresenter.headings).toHaveBeenCalledWith( + i18nService, + 'overview', + ); + expect(ListEntryPresenter.prototype.tableRow).toHaveBeenCalledTimes(3); expect(OrganisationsCheckboxPresenter).toHaveBeenCalledWith( allOrganisations, diff --git a/src/decisions/admin/publication.controller.spec.ts b/src/decisions/admin/publication.controller.spec.ts index 88fe9f6c6..9c18076ca 100644 --- a/src/decisions/admin/publication.controller.spec.ts +++ b/src/decisions/admin/publication.controller.spec.ts @@ -117,6 +117,8 @@ describe('PublicationController', () => { year: 2016, }); + decisionDatasetsService.find.mockResolvedValue(dataset); + await controller.create( 'example-profession-id', 'example-organisation-id', diff --git a/src/organisations/admin/organisation-archive.controller.spec.ts b/src/organisations/admin/organisation-archive.controller.spec.ts index d5420e194..5f61ca2f3 100644 --- a/src/organisations/admin/organisation-archive.controller.spec.ts +++ b/src/organisations/admin/organisation-archive.controller.spec.ts @@ -232,6 +232,8 @@ describe('OrganisationArchiveController', () => { version, ); + organisationVersionsService.create.mockResolvedValue(version); + await controller.delete(req, res, organisation.id, version.id); expect(checkCanViewOrganisationSpy).toHaveBeenCalledWith( diff --git a/src/organisations/admin/organisation-publication.controller.spec.ts b/src/organisations/admin/organisation-publication.controller.spec.ts index 11d5d7e1a..c8100dc95 100644 --- a/src/organisations/admin/organisation-publication.controller.spec.ts +++ b/src/organisations/admin/organisation-publication.controller.spec.ts @@ -248,6 +248,9 @@ describe('OrganisationPublicationController', () => { organisation: organisation, }); + organisationsService.find.mockResolvedValue(organisation); + organisationVersionsService.create.mockResolvedValue(version); + const res = createMock({}); const req = createDefaultMockRequest({ diff --git a/src/organisations/admin/organisation-unarchive.controller.spec.ts b/src/organisations/admin/organisation-unarchive.controller.spec.ts index 349d46e0d..35e9fd79e 100644 --- a/src/organisations/admin/organisation-unarchive.controller.spec.ts +++ b/src/organisations/admin/organisation-unarchive.controller.spec.ts @@ -215,6 +215,8 @@ describe('OrganisationUnarchiveController', () => { organisationVersionsService.findByIdWithOrganisation.mockResolvedValue( version, ); + organisationVersionsService.create.mockResolvedValue(version); + organisationsService.find.mockResolvedValue(organisation); await controller.create(req, res, organisation.id, version.id); diff --git a/src/organisations/admin/organisation-versions.controller.spec.ts b/src/organisations/admin/organisation-versions.controller.spec.ts index 074b3d623..13b960420 100644 --- a/src/organisations/admin/organisation-versions.controller.spec.ts +++ b/src/organisations/admin/organisation-versions.controller.spec.ts @@ -110,6 +110,8 @@ describe('OrganisationVersionsController', () => { version, ); + organisationVersionsService.create.mockResolvedValue(version); + organisationVersionsService.hasLiveVersion.mockResolvedValue(true); await controller.create(response, request, 'some-uuid'); diff --git a/src/organisations/admin/organisations.controller.spec.ts b/src/organisations/admin/organisations.controller.spec.ts index 846baa131..9da651a49 100644 --- a/src/organisations/admin/organisations.controller.spec.ts +++ b/src/organisations/admin/organisations.controller.spec.ts @@ -119,6 +119,11 @@ describe('OrganisationsController', () => { const nations = Nation.all(); const industries = industryFactory.buildList(5); + industriesService.all.mockResolvedValue(industries); + organisationVersionsService.searchWithLatestVersion.mockResolvedValue( + organisations, + ); + ( OrganisationsPresenter.prototype as DeepMocked ).present.mockReturnValue(templateParams); @@ -187,6 +192,11 @@ describe('OrganisationsController', () => { const nations = Nation.all(); const industries = industryFactory.buildList(5); + industriesService.all.mockResolvedValue(industries); + organisationVersionsService.searchWithLatestVersion.mockResolvedValue( + [organisations[1], organisations[3]], + ); + ( OrganisationsPresenter.prototype as DeepMocked ).present.mockReturnValue(templateParams); @@ -260,6 +270,11 @@ describe('OrganisationsController', () => { const nations = Nation.all(); const industries = industryFactory.buildList(5); + industriesService.all.mockResolvedValue(industries); + organisationVersionsService.searchWithLatestVersion.mockResolvedValue([ + userOrganisation, + ]); + const request = createDefaultMockRequest(); (getActingUser as jest.Mock).mockReturnValue( userFactory.build({ diff --git a/src/professions/admin/confirmation.controller.spec.ts b/src/professions/admin/confirmation.controller.spec.ts index 2a0891474..bb4af1004 100644 --- a/src/professions/admin/confirmation.controller.spec.ts +++ b/src/professions/admin/confirmation.controller.spec.ts @@ -140,8 +140,12 @@ describe('ConfirmationController', () => { }); const profession = professionFactory.build(); + const version = professionVersionFactory + .justCreated('version-id') + .build({ profession: profession }); professionsService.findWithVersions.mockResolvedValue(profession); + professionVersionsService.findWithProfession.mockResolvedValue(version); await controller.create(res, req, 'profession-id', 'version-id'); diff --git a/src/professions/admin/profession-archive.controller.spec.ts b/src/professions/admin/profession-archive.controller.spec.ts index 4910543a4..74ad14696 100644 --- a/src/professions/admin/profession-archive.controller.spec.ts +++ b/src/professions/admin/profession-archive.controller.spec.ts @@ -166,6 +166,8 @@ describe('ProfessionArchiveController', () => { version, ); + professionVersionsService.create.mockResolvedValue(version); + const request = createDefaultMockRequest({ user: userFactory.build(), }); diff --git a/src/professions/admin/profession-publication.controller.spec.ts b/src/professions/admin/profession-publication.controller.spec.ts index 8a9dd9e65..5d785fbfb 100644 --- a/src/professions/admin/profession-publication.controller.spec.ts +++ b/src/professions/admin/profession-publication.controller.spec.ts @@ -310,6 +310,8 @@ describe('ProfessionPublicationController', () => { version, ); + professionVersionsService.create.mockResolvedValue(version); + (getPublicationBlockers as jest.Mock).mockReturnValue([]); await controller.create(req, res, profession.id, version.id); diff --git a/src/professions/admin/profession-versions.controller.spec.ts b/src/professions/admin/profession-versions.controller.spec.ts index cab44bb1b..f473ee4c7 100644 --- a/src/professions/admin/profession-versions.controller.spec.ts +++ b/src/professions/admin/profession-versions.controller.spec.ts @@ -113,6 +113,8 @@ describe('ProfessionVersionsController', () => { version, ); + professionVersionsService.create.mockResolvedValue(version); + const res = createMock(); const req = createDefaultMockRequest({ user: userFactory.build() }); diff --git a/src/professions/admin/registration.controller.spec.ts b/src/professions/admin/registration.controller.spec.ts index b22af81df..dcbae930b 100644 --- a/src/professions/admin/registration.controller.spec.ts +++ b/src/professions/admin/registration.controller.spec.ts @@ -294,8 +294,12 @@ describe(RegistrationController, () => { it('checks the user has permission to update the Profession', async () => { const profession = professionFactory.justCreated('profession-id').build(); + const version = professionVersionFactory + .justCreated('version-id') + .build({ profession: profession }); professionsService.findWithVersions.mockResolvedValue(profession); + professionVersionsService.findWithProfession.mockResolvedValue(version); const request = createDefaultMockRequest({ user: userFactory.build(), diff --git a/src/professions/search/search.controller.spec.ts b/src/professions/search/search.controller.spec.ts index cec6cefbc..f15fb1993 100644 --- a/src/professions/search/search.controller.spec.ts +++ b/src/professions/search/search.controller.spec.ts @@ -166,6 +166,9 @@ describe('SearchController', () => { const industry1 = industryFactory.build(); const industry2 = industryFactory.build(); + const industries = [industry1, industry2]; + industriesService.all.mockResolvedValue(industries); + await controller.index( { keywords: 'example search', diff --git a/src/users/organisation/organisation.controller.spec.ts b/src/users/organisation/organisation.controller.spec.ts index 8d8c13b6d..a9a9147ae 100644 --- a/src/users/organisation/organisation.controller.spec.ts +++ b/src/users/organisation/organisation.controller.spec.ts @@ -76,9 +76,9 @@ describe('OrganisationController', () => { await expect( controller.edit(request, response, 'user-id', null), - ).rejects.toThrowError(UnauthorizedException); + ).rejects.toThrow(UnauthorizedException); - expect(checkUserIsServiceOwner).toBeCalledWith(request); + expect(checkUserIsServiceOwner).toHaveBeenCalledWith(user); }); }); @@ -132,9 +132,9 @@ describe('OrganisationController', () => { await controller.edit(request, response, user.id, null); - expect(checkUserIsServiceOwner).toBeCalledWith(request); + expect(checkUserIsServiceOwner).toHaveBeenCalledWith(serviceOwnerUser); - expect(response.render).toBeCalledWith( + expect(response.render).toHaveBeenCalledWith( 'admin/users/organisation/edit', expect.objectContaining({ action: 'edit', @@ -144,9 +144,9 @@ describe('OrganisationController', () => { }), ); - expect(usersService.find).toBeCalledWith(user.id); + expect(usersService.find).toHaveBeenCalledWith(user.id); - expect(RegulatedAuthoritiesSelectPresenter).toBeCalledWith( + expect(RegulatedAuthoritiesSelectPresenter).toHaveBeenCalledWith( [organisation1, organisation2], organisation1, null, @@ -156,7 +156,7 @@ describe('OrganisationController', () => { RegulatedAuthoritiesSelectPresenter.prototype.selectArgs, ).toBeCalled(); - expect(ServiceOwnerRadioButtonArgsPresenter).toBeCalledWith( + expect(ServiceOwnerRadioButtonArgsPresenter).toHaveBeenCalledWith( false, i18nService, ); @@ -186,11 +186,11 @@ describe('OrganisationController', () => { await expect( controller.update(request, response, 'user-id', false), - ).rejects.toThrowError(UnauthorizedException); + ).rejects.toThrow(UnauthorizedException); - expect(usersService.save).not.toBeCalled(); + expect(usersService.save).not.toHaveBeenCalled(); - expect(checkUserIsServiceOwner).toBeCalledWith(request); + expect(checkUserIsServiceOwner).toHaveBeenCalledWith(user); }); }); @@ -220,15 +220,15 @@ describe('OrganisationController', () => { await controller.update(request, response, user.id, organisationDto); - expect(checkUserIsServiceOwner).toBeCalledWith(request); + expect(checkUserIsServiceOwner).toHaveBeenCalledWith(serviceOwnerUser); - expect(organisationsService.find).toBeCalledWith(organisation.id); - expect(usersService.save).toBeCalledWith({ + expect(organisationsService.find).toHaveBeenCalledWith(organisation.id); + expect(usersService.save).toHaveBeenCalledWith({ ...user, organisation, serviceOwner: false, }); - expect(response.redirect).toBeCalledWith( + expect(response.redirect).toHaveBeenCalledWith( `/admin/users/${user.id}/personal-details/edit`, ); }); @@ -289,7 +289,7 @@ describe('OrganisationController', () => { await controller.update(request, response, user.id, organisationDto); - expect(response.render).toBeCalledWith( + expect(response.render).toHaveBeenCalledWith( 'admin/users/organisation/edit', expect.objectContaining({ action: 'edit', @@ -299,13 +299,13 @@ describe('OrganisationController', () => { }), ); - expect(checkUserIsServiceOwner).toBeCalledWith(request); + expect(checkUserIsServiceOwner).toHaveBeenCalledWith(serviceOwnerUser); - expect(usersService.find).toBeCalledWith(user.id); - expect(usersService.save).not.toBeCalled(); - expect(organisationsService.find).not.toBeCalled(); + expect(usersService.find).toHaveBeenCalledWith(user.id); + expect(usersService.save).not.toHaveBeenCalled(); + expect(organisationsService.find).not.toHaveBeenCalled(); - expect(RegulatedAuthoritiesSelectPresenter).toBeCalledWith( + expect(RegulatedAuthoritiesSelectPresenter).toHaveBeenCalledWith( [organisation1, organisation2], undefined, null, @@ -313,15 +313,15 @@ describe('OrganisationController', () => { ); expect( RegulatedAuthoritiesSelectPresenter.prototype.selectArgs, - ).toBeCalled(); + ).toHaveBeenCalled(); - expect(ServiceOwnerRadioButtonArgsPresenter).toBeCalledWith( + expect(ServiceOwnerRadioButtonArgsPresenter).toHaveBeenCalledWith( false, i18nService, ); expect( ServiceOwnerRadioButtonArgsPresenter.prototype.radioButtonArgs, - ).toBeCalled(); + ).toHaveBeenCalled(); }); it('does not check for the presence of an organisation when creating a service-owner user', async () => { @@ -347,14 +347,14 @@ describe('OrganisationController', () => { await controller.update(request, response, user.id, organisationDto); - expect(checkUserIsServiceOwner).toBeCalledWith(request); + expect(checkUserIsServiceOwner).toHaveBeenCalledWith(serviceOwnerUser); expect(organisationsService.find).not.toHaveBeenCalled(); - expect(usersService.save).toBeCalledWith({ + expect(usersService.save).toHaveBeenCalledWith({ ...user, serviceOwner: true, }); - expect(response.redirect).toBeCalledWith( + expect(response.redirect).toHaveBeenCalledWith( `/admin/users/${user.id}/personal-details/edit`, ); }); diff --git a/src/users/personal-details/personal-details.controller.spec.ts b/src/users/personal-details/personal-details.controller.spec.ts index 2795ecddf..3b7010f72 100644 --- a/src/users/personal-details/personal-details.controller.spec.ts +++ b/src/users/personal-details/personal-details.controller.spec.ts @@ -72,7 +72,7 @@ describe('PersonalDetailsController', () => { source: null, }); - expect(checkCanViewUser).toHaveBeenCalledWith(request, user.organisation); + expect(checkCanViewUser).toHaveBeenCalledWith(request, user); }); it('should set source to the given value', async () => { @@ -95,7 +95,7 @@ describe('PersonalDetailsController', () => { await controller.edit('user-uuid', null, request); - expect(checkCanViewUser).toHaveBeenCalledWith(request, user.organisation); + expect(checkCanViewUser).toHaveBeenCalledWith(request, user); }); }); @@ -107,9 +107,7 @@ describe('PersonalDetailsController', () => { }); it('should redirect to role and update the user the email address if not already in use and the body is populated', async () => { - usersService.findByEmail.mockImplementationOnce(() => { - return null; - }); + usersService.findByEmail.mockResolvedValue(null); const request = createDefaultMockRequest({ user: userFactory.build() }); @@ -131,9 +129,7 @@ describe('PersonalDetailsController', () => { }); it('should correct a mis-formatted email address before saving', async () => { - usersService.findByEmail.mockImplementationOnce(() => { - return null; - }); + usersService.findByEmail.mockResolvedValue(null); const request = createDefaultMockRequest({ user: userFactory.build() }); @@ -295,7 +291,7 @@ describe('PersonalDetailsController', () => { request, ); - expect(checkCanViewUser).toHaveBeenCalledWith(request, user.organisation); + expect(checkCanViewUser).toHaveBeenCalledWith(request, user); }); }); });