Skip to content

Commit 76bf9e9

Browse files
committed
test(public-notice): add regression tests for null prefill and service query
1 parent c85341d commit 76bf9e9

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

admin/src/app/foms/public-notice/public-notice-edit.component.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ describe('PublicNoticeEditComponent', () => {
3535
let fixture: ComponentFixture<PublicNoticeEditComponent>;
3636
let findOneMock: jest.Mock;
3737
let findLatestMock: jest.Mock;
38+
let createMock: jest.Mock;
39+
let updateMock: jest.Mock;
3840
let router: Router;
3941

4042
const existingNotice = {
@@ -82,6 +84,8 @@ describe('PublicNoticeEditComponent', () => {
8284
beforeEach(async () => {
8385
findOneMock = jest.fn().mockReturnValue(asyncOf(existingNotice));
8486
findLatestMock = jest.fn().mockReturnValue(asyncOf(clientLatestNotice));
87+
createMock = jest.fn().mockReturnValue(asyncOf({ id: 101 }));
88+
updateMock = jest.fn().mockReturnValue(asyncOf({ id: 55 }));
8589

8690
await TestBed.configureTestingModule({
8791
imports: [PublicNoticeEditComponent, NoopAnimationsModule],
@@ -92,6 +96,8 @@ describe('PublicNoticeEditComponent', () => {
9296
useValue: {
9397
publicNoticeControllerFindOne: findOneMock,
9498
publicNoticeControllerFindLatestPublicNotice: findLatestMock,
99+
publicNoticeControllerCreate: createMock,
100+
publicNoticeControllerUpdate: updateMock,
95101
},
96102
},
97103
{ provide: CognitoService, useValue: { getUser: () => ({ isForestClient: true, isAuthorizedForClientId: () => true }) } },
@@ -165,13 +171,24 @@ describe('PublicNoticeEditComponent', () => {
165171
expect(findOneMock).not.toHaveBeenCalled();
166172
});
167173

168-
it('is treated as a new notice and renders an empty form', () => {
174+
it('is treated as a new notice and renders an empty form with actions', () => {
169175
expect(component.isNewForm).toBe(true);
170176
expect(component.isAddNewNotice()).toBe(true);
171177
expect(component.publicNoticeResponse).toBeNull();
172178
expect(component.formReady()).toBe(true);
173179
expect(component.publicNoticeFormGroup).toBeDefined();
174180
expect(fixture.nativeElement.querySelector('form#publicNoticeForm')).not.toBeNull();
181+
expect(fixture.nativeElement.querySelector('h1').textContent).toContain('New Online Public Notice');
182+
expect(fixture.nativeElement.querySelector('h1').textContent).toContain('FOM Number: 2');
183+
expect(fixture.nativeElement.querySelector('button[type="submit"]')).not.toBeNull();
184+
});
185+
186+
it('submits a new public notice on valid form submission and navigates back', async () => {
187+
const navSpy = jest.spyOn(router, 'navigate').mockResolvedValue(true);
188+
await component.onSubmit();
189+
expect(createMock).toHaveBeenCalledWith(expect.objectContaining({ projectId: 2 }));
190+
expect(updateMock).not.toHaveBeenCalled();
191+
expect(navSpy).toHaveBeenCalledWith(['/a', 2]);
175192
});
176193
});
177194

api/src/app/modules/project/public-notice.service.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,57 @@ describe('PublicNoticeService', () => {
218218

219219
});
220220

221+
describe('findLatestPublicNotice', () => {
222+
beforeEach(async () => {
223+
user = new User();
224+
});
225+
226+
it('throws ForbiddenException when user is not authorized', async () => {
227+
user.isForestClient = true;
228+
user.clientIds = ['different-client-id'];
229+
await expect(service.findLatestPublicNotice('00001012', user)).rejects.toThrow();
230+
});
231+
232+
it('returns null when no public notice is found for forest client', async () => {
233+
user.isForestClient = true;
234+
user.clientIds = ['00001012'];
235+
const createQueryBuilder: any = {
236+
select: () => createQueryBuilder,
237+
addSelect: () => createQueryBuilder,
238+
innerJoin: () => createQueryBuilder,
239+
where: () => createQueryBuilder,
240+
addOrderBy: () => createQueryBuilder,
241+
limit: () => createQueryBuilder,
242+
getOne: () => null,
243+
};
244+
jest.spyOn(repository, 'createQueryBuilder').mockImplementation(() => createQueryBuilder);
245+
246+
const result = await service.findLatestPublicNotice('00001012', user);
247+
expect(result).toBeNull();
248+
});
249+
250+
it('returns converted public notice response when a notice is found', async () => {
251+
user.isForestClient = true;
252+
user.clientIds = ['00001012'];
253+
const sampleNotice = getSamplePublicNoticeEntity();
254+
const createQueryBuilder: any = {
255+
select: () => createQueryBuilder,
256+
addSelect: () => createQueryBuilder,
257+
innerJoin: () => createQueryBuilder,
258+
where: () => createQueryBuilder,
259+
addOrderBy: () => createQueryBuilder,
260+
limit: () => createQueryBuilder,
261+
getOne: () => sampleNotice,
262+
};
263+
jest.spyOn(repository, 'createQueryBuilder').mockImplementation(() => createQueryBuilder);
264+
265+
const result = await service.findLatestPublicNotice('00001012', user);
266+
expect(result).toBeDefined();
267+
expect(result.id).toBe(sampleNotice.id);
268+
expect(result.projectId).toBe(sampleNotice.projectId);
269+
});
270+
});
271+
221272
});
222273

223274
export class PublicNoticeRepositoryFake {

0 commit comments

Comments
 (0)