-
Notifications
You must be signed in to change notification settings - Fork 703
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
Showing
7 changed files
with
174 additions
and
43 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,79 @@ | ||
import { LocalStorage } from './localstorage'; | ||
import { State, Server } from '../../types'; | ||
|
||
// this is not best place for fake string generator | ||
|
||
const getRandomString = () => | ||
Math.random() | ||
.toString(36) | ||
.substring(7); | ||
|
||
describe('LocaLStorage', () => { | ||
let db: LocalStorage; | ||
|
||
beforeAll(() => { | ||
db = new LocalStorage(getRandomString()); | ||
db.connect(); | ||
}); | ||
|
||
// Must return undefined when no game exists. | ||
test('must return undefined when no game exists', () => { | ||
const { state } = db.fetch('gameID', { state: true }); | ||
expect(state).toEqual(undefined); | ||
}); | ||
|
||
test('create game', () => { | ||
let stateEntry: unknown = { a: 1 }; | ||
|
||
// Create game. | ||
db.createMatch('gameID', { | ||
metadata: { | ||
gameName: 'tic-tac-toe', | ||
} as Server.MatchData, | ||
initialState: stateEntry as State, | ||
}); | ||
|
||
// Must return created game. | ||
const { state } = db.fetch('gameID', { state: true }); | ||
expect(state).toEqual(stateEntry); | ||
|
||
// Fetch initial state. | ||
const { initialState } = db.fetch('gameID', { initialState: true }); | ||
expect(initialState).toEqual(stateEntry); | ||
}); | ||
|
||
test('listGames', () => { | ||
let keys = db.listMatches({}); | ||
expect(keys).toEqual(['gameID']); | ||
keys = db.listMatches({ gameName: 'tic-tac-toe' }); | ||
expect(keys).toEqual(['gameID']); | ||
keys = db.listMatches({ gameName: 'chess' }); | ||
expect(keys).toEqual([]); | ||
}); | ||
|
||
test('remove', () => { | ||
// Must remove game from DB | ||
db.wipe('gameID'); | ||
expect(db.fetch('gameID', { state: true })).toEqual({}); | ||
// Shall not return error | ||
db.wipe('gameID'); | ||
}); | ||
|
||
test('must create new empty db if other localstorage key is used', () => { | ||
// create another localstorage with anothr key | ||
let db2 = new LocalStorage(getRandomString()); | ||
let stateEntry: unknown = { a: 1 }; | ||
|
||
// create game in db | ||
db.createMatch('gameID', { | ||
metadata: { | ||
gameName: 'tic-tac-toe', | ||
} as Server.MatchData, | ||
initialState: stateEntry as State, | ||
}); | ||
|
||
// game shouldnt be visible in db2 | ||
const { state } = db2.fetch('gameID', { state: true }); | ||
expect(state).toEqual(undefined); | ||
}); | ||
}); |
Oops, something went wrong.