-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(wizards): add profiles on enter test
- Loading branch information
1 parent
3762bb6
commit 1b9c25e
Showing
3 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`), | ||
}, | ||
); | ||
}); | ||
}); | ||
}); |