Skip to content

Commit

Permalink
Fixes Transformer.controller specs (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
1010varun authored Aug 2, 2023
1 parent c6fade4 commit bbcfe22
Showing 1 changed file with 138 additions and 10 deletions.
148 changes: 138 additions & 10 deletions src/modules/transformer/transformer.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,148 @@
import { TransformerService } from './transformer.service';
import { TransformerController } from './transformer.controller';
import { Test, TestingModule } from '@nestjs/testing';
import { ConversationLogicController } from '../conversation-logic/conversation-logic.controller';
import { ConversationLogicService } from '../conversation-logic/conversation-logic.service';
import { AddOwnerInfoInterceptor } from '../../interceptors/addOwnerInfo.interceptor';
import { AddAdminHeaderInterceptor } from '../../interceptors/addAdminHeader.interceptor';
import { createMock } from '@golevelup/ts-jest';

describe.skip('ConversationLogicController', () => {
let controller: ConversationLogicController;
describe('TransformerController', () => {
let controller: TransformerController;

const mockTransformerData = {
id: '123',
createdAt: 1000,
updatedAt: 1000,
name: 'mockTransformerService',
service: { create: 'create' },
tags: ['tag1', 'tag2'],
config: {
value: '123',
},
};

const mockTransformerService = {
create: jest.fn((data) => {
return {
id: '123',
createdAt: 1000,
updatedAt: 1000,
name: data.name,
service: { create: data.service },
tags: data.tags,
config: data.config,
};
}),

findAll: jest.fn(() => {
return [
{
...mockTransformerData,
},
];
}),

findOne: jest.fn((id) => {
const res = {
id,
createdAt: 1000,
updatedAt: 1000,
name: 'mockTransformerService',
service: { create: 'create' },
tags: ['tag1', 'tag2'],
config: {
value: '123',
},
};
return { ...res };
}),

update: jest.fn((id, data) => {
return {
id,
createdAt: 1000,
updatedAt: 1000,
name: data.name,
service: { create: data.service },
tags: data.tags,
config: data.config,
};
}),

remove: jest.fn((id) => {
return {
id,
};
}),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ConversationLogicController],
providers: [ConversationLogicService],
}).compile();
controllers: [TransformerController],
providers: [TransformerService],
})
.overrideProvider(TransformerService)
.useValue(mockTransformerService)
.overrideInterceptor(AddAdminHeaderInterceptor)
.useValue(createMock<AddAdminHeaderInterceptor>())
.overrideInterceptor(AddOwnerInfoInterceptor)
.useValue(createMock<AddOwnerInfoInterceptor>())
.compile();

controller = module.get<TransformerController>(TransformerController);
});

describe('create', () => {
it('should create a new transformer object', () => {
const data = {
name: 'mockTransformerService',
service: 'create',
tags: ['tag1', 'tag2'],
config: {
value: '123',
},
};
expect(controller.create(data)).toEqual(mockTransformerData);
expect(mockTransformerService.create).toBeCalled;
});
});

describe('findAll', () => {
it('should return the array of transformer objects', () => {
expect(controller.findAll()).toEqual([{ ...mockTransformerData }]);
expect(controller.findAll).toBeCalled;
});
});

describe('findOne', () => {
it('should return the specific transformer object', () => {
expect(controller.findOne('123')).toEqual({ ...mockTransformerData });
expect(mockTransformerService.findOne).toBeCalled;
});
});

controller = module.get<ConversationLogicController>(ConversationLogicController);
describe('update', () => {
it('should return the updated transformer object', () => {
const mockResponse = { ...mockTransformerData };
mockResponse.name = 'updatedName';
expect(
controller.update('123', {
name: 'updatedName',
service: 'create',
tags: ['tag1', 'tag2'],
config: {
value: '123',
},
}),
).toEqual(mockResponse);
expect(mockTransformerService.update).toBeCalled;
});
});

it('should be defined', () => {
expect(controller).toBeDefined();
describe('remove', () => {
it('should successfully delete a transformer object', () => {
const id = '123';
expect(controller.remove('123')).toEqual({ id });
expect(mockTransformerService.remove).toBeCalled;
});
});
});

0 comments on commit bbcfe22

Please sign in to comment.