Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user-segment.service specs #195

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Comment on lines +8 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @1010varun this PR is supposed to contain test fixes for user-segment.service but it contains changes for transformer.controller as well which as addressed on this PR. Can you clean this PR up to only contain the necessary changes after rebasing from the latest develop branch?

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;
});
});
});
145 changes: 141 additions & 4 deletions src/modules/user-segment/user-segment.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,155 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserSegmentService } from './user-segment.service';
import { PrismaService } from '../../global-services/prisma.service';

describe.skip('UserSegmentService', () => {
describe('UserSegmentService', () => {
let service: UserSegmentService;

const mockUserSegment = {
id: '123',
createdAt: 10000,
updatedAt: 10000,
name: 'mockUserSegment',
all: {
connect: {
id: 7,
},
},
byPhone: {
connect: {
id: 8,
},
},
byId: {
connect: {
id: 9,
},
},
};

const mockUserSegmentService = {
userSegment: {
create: jest.fn(async (userSegment) => {
return {
id: '123',
createdAt: 10000,
updatedAt: 10000,
name: userSegment.data.name,
all: {
connect: {
id: 7,
},
},
byPhone: {
connect: {
id: 8,
},
},
byId: {
connect: {
id: 9,
},
},
};
}),

findUnique: jest.fn((filter) => {
const resp = { ...mockUserSegment };
resp.id = filter.where.id;
return resp;
}),

findMany: jest.fn().mockReturnValue([{ ...mockUserSegment }]),
},
adapter: {
update: jest.fn((payload) => {
return {
id: payload.where.id,
createdAt: 10000,
updatedAt: 10000,
...payload.data,
};
}),
},
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UserSegmentService],
providers: [
UserSegmentService,
{
provide: PrismaService,
useValue: mockUserSegmentService,
},
],
}).compile();

service = module.get<UserSegmentService>(UserSegmentService);
});

it('should be defined', () => {
expect(service).toBeDefined();
describe('create', () => {
it('should successfully create a user segment', async () => {
const userSegment = {
name: 'mockUserSegment',
all: 7,
phone: 8,
ID: 9,
};
expect(await service.create(userSegment)).toEqual({ ...mockUserSegment });
expect(mockUserSegmentService.userSegment.create).toHaveBeenCalled;
});
});

describe('findOne', () => {
it('should return specific user info', () => {
const resp = { ...mockUserSegment };
resp.id = '567';
expect(service.findOne('567')).toEqual({ ...resp });
expect(mockUserSegmentService.userSegment.findUnique).toHaveBeenCalled;
});
});

describe('findAll', () => {
it('should return array of all user segment object', () => {
expect(service.findAll()).toEqual([{ ...mockUserSegment }]);
expect(mockUserSegmentService.userSegment.findMany).toHaveBeenCalled;
});
});

describe('update', () => {
it('should successfully update user segment', () => {
const resp = { ...mockUserSegment };
resp.name = 'updatedName';
expect(
service.update('123', {
name: 'updatedName',
all: {
connect: {
id: 7,
},
},
byPhone: {
connect: {
id: 8,
},
},
byId: {
connect: {
id: 9,
},
},
}),
).toEqual({ ...resp });
expect(mockUserSegmentService.adapter.update).toHaveBeenCalled;
});
});

describe('remove', () => {
it('should remove user segment object', () => {
const id = '123';
expect(service.remove('123')).toEqual(
`This action removes a #${id} adapter`,
);
});
});
});
Loading