-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b01dd75
commit f980e8e
Showing
2 changed files
with
82 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Environment } from '@/environment/environment'; | ||
import { Player } from '@/players/models/player'; | ||
import { PlayersService } from '@/players/services/players.service'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { mongoose } from '@typegoose/typegoose'; | ||
import { SteamStrategy } from './steam.strategy'; | ||
|
||
const mockPlayer: Player = { | ||
id: 'FAKE_PLAYER_ID', | ||
name: 'FAKE_PLAYER', | ||
steamId: 'FAKE_STEAM_ID', | ||
hasAcceptedRules: true, | ||
}; | ||
|
||
jest.mock('@/players/services/players.service', () => ({ | ||
PlayersService: jest.fn().mockImplementation(() => ({ | ||
findBySteamId: jest.fn().mockResolvedValue(mockPlayer), | ||
createPlayer: jest.fn().mockResolvedValue({ ...mockPlayer, hasAcceptedRules: false }), | ||
updatePlayer: jest.fn().mockResolvedValue(mockPlayer), | ||
})), | ||
})); | ||
|
||
const environment = { | ||
apiUrl: 'http://api.tf2pickup.pl', | ||
steamApiKey: 'FAKE_STEAM_API_KEY', | ||
}; | ||
|
||
describe('SteamStrategy', () => { | ||
let strategy: SteamStrategy; | ||
let playersService: jest.Mocked<PlayersService>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
SteamStrategy, | ||
PlayersService, | ||
{ provide: Environment, useValue: environment }, | ||
], | ||
}).compile(); | ||
|
||
strategy = module.get<SteamStrategy>(SteamStrategy); | ||
playersService = module.get(PlayersService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(strategy).toBeDefined(); | ||
}); | ||
|
||
describe('#validate()', () => { | ||
describe('when the player does have an account', () => { | ||
it('should return that player\'s profile', async () => { | ||
const player = await strategy.validate('FAKE_STEAM_ID', { provider: 'steam', id: 'FAKE_STEAM_ID', displayName: 'FAKE_PLAYER', photos: [] }); | ||
expect(playersService.createPlayer).not.toHaveBeenCalled(); | ||
expect(player).toEqual(mockPlayer); | ||
}); | ||
}); | ||
|
||
describe('when the player does not have an account', () => { | ||
beforeEach(() => { | ||
playersService.findBySteamId.mockImplementation(steamId => { | ||
throw new mongoose.Error.DocumentNotFoundError({ steamId }); | ||
}); | ||
}); | ||
|
||
it('should create it', async () => { | ||
const player = await strategy.validate('FAKE_STEAM_ID', { provider: 'steam', id: 'FAKE_STEAM_ID', displayName: 'FAKE_PLAYER', photos: [] }); | ||
expect(playersService.createPlayer).toHaveBeenCalled(); | ||
expect(player.steamId).toEqual('FAKE_STEAM_ID'); | ||
expect(player.hasAcceptedRules).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); |
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