-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented repository.expiredAt method
- Loading branch information
1 parent
3ffbedb
commit 0c09089
Showing
2 changed files
with
109 additions
and
0 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
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,72 @@ | ||
import '../helpers/mock-client'; | ||
|
||
import { Client } from '$lib/client'; | ||
import { Repository } from '$lib/repository'; | ||
import { Schema } from '$lib/schema'; | ||
|
||
const simpleSchema = new Schema('SimpleEntity', {}, { dataStructure: 'HASH' }); | ||
|
||
const today = new Date(); | ||
const tomorrow = new Date(today); | ||
tomorrow.setDate(tomorrow.getDate() + 1); | ||
const yesterday = new Date(today); | ||
yesterday.setDate(yesterday.getDate() - 1); | ||
|
||
describe('Repository', () => { | ||
describe('#expireAt', () => { | ||
let client: Client; | ||
let repository: Repository; | ||
|
||
beforeAll(() => { | ||
client = new Client(); | ||
}); | ||
beforeEach(() => { | ||
repository = new Repository(simpleSchema, client); | ||
}); | ||
|
||
it('expires a single entity', async () => { | ||
const ttlInSeconds: number = Math.round( | ||
(tomorrow.getTime() - Date.now()) / 1000 | ||
); | ||
await repository.expireAt('foo', tomorrow); | ||
expect(client.expire).toHaveBeenCalledWith( | ||
'SimpleEntity:foo', | ||
ttlInSeconds | ||
); | ||
}); | ||
|
||
it('expires multiple entities', async () => { | ||
const ttlInSeconds: number = Math.round( | ||
(tomorrow.getTime() - Date.now()) / 1000 | ||
); | ||
await repository.expireAt(['foo', 'bar', 'baz'], tomorrow); | ||
expect(client.expire).toHaveBeenNthCalledWith( | ||
1, | ||
'SimpleEntity:foo', | ||
ttlInSeconds | ||
); | ||
expect(client.expire).toHaveBeenNthCalledWith( | ||
2, | ||
'SimpleEntity:bar', | ||
ttlInSeconds | ||
); | ||
expect(client.expire).toHaveBeenNthCalledWith( | ||
3, | ||
'SimpleEntity:baz', | ||
ttlInSeconds | ||
); | ||
}); | ||
|
||
it('throws an error when provided invalid/past date', async () => { | ||
let caughtError: any; | ||
await repository.expireAt('foo', yesterday).catch((error) => { | ||
caughtError = error; | ||
}); | ||
expect(client.expire).toHaveBeenCalledTimes(0); | ||
expect(caughtError).toBeDefined(); | ||
expect(caughtError!.message).toEqual( | ||
`${yesterday.toString()} is invalid. Expiration date must be in the future.` | ||
); | ||
}); | ||
}); | ||
}); |