Files
lolympus-tournament-support/apps/api/src/modules/dakao/handlers/match-cancel.handler.spec.ts
T

169 lines
5.1 KiB
TypeScript

import { BadRequestException } from '@nestjs/common';
import mongoose from 'mongoose';
import { MatchService } from '../../match/match.service';
import { DakaoUserService } from '../dakao-user.service';
import { MatchCancelHandler } from './match-cancel.handler';
const objectId = (hex: string) => new mongoose.Types.ObjectId(hex);
function makeHandler(overrides?: {
matchService?: Partial<MatchService>;
dakaoUserService?: Partial<DakaoUserService>;
}): MatchCancelHandler {
const matchService = {
cancelJoin: jest.fn(),
list: jest.fn(),
...overrides?.matchService,
} as unknown as MatchService;
const dakaoUserService = {
findUserByKakaoTalkId: jest.fn(),
...overrides?.dakaoUserService,
} as unknown as DakaoUserService;
return new MatchCancelHandler(matchService, dakaoUserService);
}
const baseParams = {
communityId: objectId('111111111111111111111111'),
matchId: '69b63bc96da18dfdb218ee8c',
senderUserId: 'kakao-user-1',
senderNickname: '프리리',
};
describe('MatchCancelHandler', () => {
it('rejects unknown match reference without cancelling', async () => {
const cancelJoin = jest.fn();
const handler = makeHandler({
matchService: {
cancelJoin,
list: jest.fn().mockResolvedValue([]),
} as Partial<MatchService>,
dakaoUserService: {
findUserByKakaoTalkId: jest.fn().mockResolvedValue({
_id: objectId('222222222222222222222222'),
}),
} as Partial<DakaoUserService>,
});
const segments = await handler.handle({
...baseParams,
matchId: 'not-an-id',
});
expect(cancelJoin).not.toHaveBeenCalled();
expect(segments).toEqual([
expect.objectContaining({ kind: 'mention', userId: 'kakao-user-1' }),
expect.objectContaining({
kind: 'text',
text: expect.stringContaining('모집 중인 내전이 없습니다'),
}),
]);
});
it('replies with success for registered user cancellation', async () => {
const cancelJoin = jest.fn().mockResolvedValue({
title: '저녁 내전',
participants: [{}, {}],
maxParticipants: 10,
});
const findUserByKakaoTalkId = jest.fn().mockResolvedValue({
_id: objectId('222222222222222222222222'),
});
const handler = makeHandler({
matchService: { cancelJoin } as Partial<MatchService>,
dakaoUserService: {
findUserByKakaoTalkId,
} as Partial<DakaoUserService>,
});
const segments = await handler.handle(baseParams);
expect(findUserByKakaoTalkId).toHaveBeenCalledWith({
communityId: baseParams.communityId,
kakaoTalkId: 'kakao-user-1',
});
expect(cancelJoin).toHaveBeenCalledWith({
matchId: expect.any(mongoose.Types.ObjectId),
userId: expect.any(mongoose.Types.ObjectId),
});
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('참가 취소 완료 (2/10)'),
});
});
it('resolves cancellation by short code', async () => {
const matchId = objectId('333333333333333333333333');
const cancelJoin = jest.fn().mockResolvedValue({
title: '저녁 내전',
participants: [{}, {}],
maxParticipants: 10,
});
const findUserByKakaoTalkId = jest.fn().mockResolvedValue({
_id: objectId('222222222222222222222222'),
});
const handler = makeHandler({
matchService: {
cancelJoin,
list: jest.fn().mockResolvedValue([
{ _id: matchId, title: '저녁 내전', shortCode: 'AB12' },
]),
} as Partial<MatchService>,
dakaoUserService: {
findUserByKakaoTalkId,
} as Partial<DakaoUserService>,
});
await handler.handle({
...baseParams,
matchId: 'AB12',
});
expect(cancelJoin).toHaveBeenCalledWith({
matchId,
userId: expect.any(mongoose.Types.ObjectId),
});
});
it('asks unregistered user to register first', async () => {
const cancelJoin = jest.fn();
const handler = makeHandler({
matchService: { cancelJoin } as Partial<MatchService>,
dakaoUserService: {
findUserByKakaoTalkId: jest.fn().mockResolvedValue(null),
} as Partial<DakaoUserService>,
});
const segments = await handler.handle(baseParams);
expect(cancelJoin).not.toHaveBeenCalled();
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('계정을 등록'),
});
});
it('translates not-joined cancellation errors', async () => {
const handler = makeHandler({
matchService: {
cancelJoin: jest
.fn()
.mockRejectedValue(
new BadRequestException('User has not joined this match.'),
),
} as Partial<MatchService>,
dakaoUserService: {
findUserByKakaoTalkId: jest.fn().mockResolvedValue({
_id: objectId('222222222222222222222222'),
}),
} as Partial<DakaoUserService>,
});
const segments = await handler.handle(baseParams);
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('신청한 내전이 아닙니다'),
});
});
});