Skip to content

Commit

Permalink
test(wizards): add profiles on enter test
Browse files Browse the repository at this point in the history
  • Loading branch information
havrydotdev committed Nov 1, 2023
1 parent 3762bb6 commit 1b9c25e
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/controllers/wizards/profiles.wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ProfilesMessageContext } from 'src/types';
import { ProfileUseCases } from 'src/use-cases/profile';
import { deunionize } from 'telegraf';

// TODO
@Wizard(PROFILES_WIZARD_ID)
export class ProfilesWizard {
constructor(private readonly profileUseCases: ProfileUseCases) {}
Expand Down
37 changes: 37 additions & 0 deletions src/controllers/wizards/tests/next.wizard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createMock } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { MAIN_MENU_MARKUP } from 'src/core/constants';
import { MessageContext } from 'src/types';
import { NextActionWizard } from '../next.wizard';

describe('NextActionWizard', () => {
let wizard: NextActionWizard;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [NextActionWizard],
}).compile();

wizard = module.get<NextActionWizard>(NextActionWizard);
});

describe('onEnter', () => {
it('should leave the scene and return the next action message', async () => {
const ctx = createMock<MessageContext>({
scene: {
leave: jest.fn(),
},
});

const result = await wizard.onEnter(ctx);

expect(ctx.scene.leave).toHaveBeenCalled();
expect(result).toEqual([
'messages.next_action',
{
reply_markup: MAIN_MENU_MARKUP,
},
]);
});
});
});
97 changes: 97 additions & 0 deletions src/controllers/wizards/tests/profiles.wizard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { createMock } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { File, Profile, User } from 'src/core/entities';
import { getCaption, getProfileMarkup } from 'src/core/utils';
import { ProfilesMessageContext } from 'src/types';
import { ProfileUseCases } from 'src/use-cases/profile';
import { deunionize } from 'telegraf';
import { ChatFromGetChat } from 'telegraf/typings/core/types/typegram';
import { ProfilesWizard } from '../profiles.wizard';

jest.mock('telegraf', () => {
const originalModule = jest.requireActual('telegraf');

//Mock the default export and named export 'foo'
return {
__esModule: true,
...originalModule,
deunionize: jest.fn(),
};
});

describe('ProfilesWizard', () => {
let wizard: ProfilesWizard;
let profileUseCases: ProfileUseCases;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ProfilesWizard,
{
provide: ProfileUseCases,
useValue: createMock<ProfileUseCases>(),
},
],
}).compile();

wizard = module.get<ProfilesWizard>(ProfilesWizard);
profileUseCases = module.get<ProfileUseCases>(ProfileUseCases);
});

describe('onEnter', () => {
it('should leave the scene and enter the PROFILES_WIZARD_ID scene when the NEXT_PROFILE_CALLBACK is received', async () => {
(deunionize as jest.Mock).mockReturnValue({
username: 'test',
});

const ctx = createMock<ProfilesMessageContext>({
scene: {
leave: jest.fn(),
enter: jest.fn(),
},
wizard: {
state: {},
next: jest.fn(),
},
session: {
user: createMock<User>({
profile: createMock<Profile>({
id: 2,
name: 'test2',
}),
}),
},
telegram: {
getChat: jest
.fn()
.mockResolvedValueOnce(createMock<ChatFromGetChat>()),
},
});
const recommended = createMock<Profile>({
id: 1,
name: 'test',
file: createMock<File>({
url: 'https://test.com',
}),
games: [],
});

const findSpy = jest
.spyOn(profileUseCases, 'findRecommended')
.mockResolvedValueOnce(recommended);
await wizard.onEnter(ctx);

expect(findSpy).toHaveBeenCalledWith(ctx.session.user.profile);
expect(ctx.wizard.next).toHaveBeenCalled();
expect(ctx.replyWithPhoto).toHaveBeenCalledWith(
{
url: recommended.file.url,
},
{
caption: getCaption(recommended),
reply_markup: getProfileMarkup(`https://t.me/test`),
},
);
});
});
});

0 comments on commit 1b9c25e

Please sign in to comment.