Skip to content

Commit

Permalink
chore: add controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeppner-dataport committed Sep 26, 2023
1 parent 51e8d3b commit c040616
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { ICurrentUser } from '@src/modules/authentication';
import { DemoSchoolUc } from '../uc';
import { DemoSchoolController } from './demo-school.controller';
import { DemoSchoolResponse } from './dto';

describe(DemoSchoolController.name, () => {
let module: TestingModule;
let controller: DemoSchoolController;
let uc: DeepMocked<DemoSchoolUc>;

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
{
provide: DemoSchoolUc,
useValue: createMock<DemoSchoolUc>(),
},
],
controllers: [DemoSchoolController],
}).compile();

controller = module.get(DemoSchoolController);
uc = module.get(DemoSchoolUc);
});

afterAll(async () => {
await module.close();
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('createDemoSchool', () => {
describe('when a demo school should be created via API call', () => {
const setup = () => {
const currentUser = { userId: 'userId' } as ICurrentUser;
const ucResult = {
id: 'aSchoolsId',
type: 'school', // WIP: make it an enum
key: 'my own school',
children: [],
} as DemoSchoolResponse;
uc.createSchool.mockResolvedValue(ucResult);
return { currentUser, ucResult };
};

it('should call uc', async () => {
const { currentUser } = setup();

await controller.createDemoSchool(currentUser);

expect(uc.createSchool).toHaveBeenCalled();
});

it('should return a valid response', async () => {
const { currentUser, ucResult } = setup();

const response = await controller.createDemoSchool(currentUser);

expect(response.constructor.name).toEqual(DemoSchoolResponse.name);
expect(response).toEqual(
expect.objectContaining({ id: ucResult.id, type: ucResult.type, children: ucResult.children })
);
});
});
});
});

0 comments on commit c040616

Please sign in to comment.