Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create memoryStorage.test.ts #376

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/storage/__tests__/memoryStorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Cache } from '../../types'
import getMemoryStorage from '../memoryStorage'

import mockdate from 'mockdate'

describe('memoryStorage cache', () => {
let cache: Cache
const cacheLife = 3600000 // an hour

beforeEach(() => {
cache = getLocalStorage({ cacheLife })
})

afterAll((): void => {
mockdate.reset()
})

beforeAll((): void => {
mockdate.set('2020-01-01T00:00:00.000Z')
})

it('stores and recreates response', async () => {
const body = 'response body'
const status = 200
const statusText = 'OK'
const headers = new Headers({ 'content-type': 'application/json' })
const response = new Response(
body,
{
status,
statusText,
headers
}
)
const responseID = 'aID'

await cache.set(responseID, response)
const received = await cache.get(responseID) as Response

expect(await received.text()).toEqual(body)
expect(received.ok).toBeTruthy()
expect(received.status).toEqual(status)
expect(received.statusText).toEqual(statusText)
expect(received.headers.get('content-type')).toEqual('application/json')
})

it('clears cache on expiration', async () => {
const body = 'response body'
const status = 200
const statusText = 'OK'
const headers = new Headers({ 'content-type': 'application/json' })
const response = new Response(
body,
{
status,
statusText,
headers
}
)
const responseID = 'aID'

await cache.set(responseID, response)
mockdate.set('2020-01-01T02:00:00.000Z')
await cache.get(responseID)

expect('{}').toEqual('{}')//TODO
})