test: more tests
This commit is contained in:
parent
c439b7e70a
commit
8b80b03ba1
|
@ -1,5 +1,7 @@
|
|||
import '@testing-library/jest-dom/extend-expect'
|
||||
import 'fake-indexeddb/auto'
|
||||
import { Crypto } from '@peculiar/webcrypto'
|
||||
jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox())
|
||||
|
||||
global.fetch = require('node-fetch')
|
||||
global.crypto = new Crypto()
|
||||
|
|
|
@ -12,6 +12,7 @@ module.exports = {
|
|||
transformIgnorePatterns: ['node_modules'],
|
||||
bail: false,
|
||||
verbose: true,
|
||||
silent: true,
|
||||
setupFilesAfterEnv: [
|
||||
'<rootDir>/config/jest.setup.js'
|
||||
],
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
"homepage": "https://github.com/nolanlawson/lite-emoji-picker#readme",
|
||||
"devDependencies": {
|
||||
"@babel/preset-env": "^7.10.2",
|
||||
"@peculiar/webcrypto": "^1.1.1",
|
||||
"@rollup/plugin-commonjs": "^11.1.0",
|
||||
"@rollup/plugin-json": "^4.0.3",
|
||||
"@rollup/plugin-node-resolve": "^7.1.3",
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import Database from '../Database'
|
||||
import allEmoji from 'emojibase-data/en/data.json'
|
||||
|
||||
const { Response } = fetch
|
||||
|
||||
function truncateEmoji (allEmoji) {
|
||||
// just take the first few emoji from each category, or else it takes forever to insert
|
||||
// into fake-indexeddb: https://github.com/dumbmatter/fakeIndexedDB/issues/44
|
||||
|
@ -22,21 +24,22 @@ const truncatedEmoji = truncateEmoji(allEmoji)
|
|||
|
||||
const ALL_EMOJI = 'http://localhost/emoji.json'
|
||||
const ALL_EMOJI_NO_ETAG = 'http://localhost/emoji-no-etag.json'
|
||||
const ALL_EMOJI_MISCONFIGURED_ETAG = 'http://localhost/emoji-misconfigured-etag.json'
|
||||
|
||||
beforeEach(() => {
|
||||
fetch
|
||||
.get(ALL_EMOJI, () => {
|
||||
const resp = new fetch.Response(JSON.stringify(truncatedEmoji))
|
||||
resp.headers.set('ETag', 'W/xxx')
|
||||
resp.headers.set('Content-Type', 'application/json')
|
||||
return resp
|
||||
})
|
||||
.head(ALL_EMOJI, () => {
|
||||
const resp = new fetch.Response()
|
||||
resp.headers.set('ETag', 'W/xxx')
|
||||
return resp
|
||||
})
|
||||
.get(ALL_EMOJI, () => new Response(JSON.stringify(truncatedEmoji), {
|
||||
headers: { ETag: 'W/xxx' }
|
||||
}))
|
||||
.head(ALL_EMOJI, () => new Response(null, {
|
||||
headers: { ETag: 'W/xxx' }
|
||||
}))
|
||||
.get(ALL_EMOJI_NO_ETAG, truncatedEmoji)
|
||||
.head(ALL_EMOJI_NO_ETAG, () => new Response(null))
|
||||
.get(ALL_EMOJI_MISCONFIGURED_ETAG, () => new Response(JSON.stringify(truncatedEmoji), {
|
||||
headers: { ETag: 'W/xxx' }
|
||||
}))
|
||||
.head(ALL_EMOJI_MISCONFIGURED_ETAG, () => new Response(null))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
|
@ -88,9 +91,52 @@ describe('database tests', () => {
|
|||
await db.close()
|
||||
db = new Database({ dataSource: ALL_EMOJI })
|
||||
await db.ready()
|
||||
await new Promise(resolve => setTimeout(resolve, 50)) // the HEAD request is done asynchronously, so wait for it
|
||||
await new Promise(resolve => setTimeout(resolve)) // the HEAD request is done asynchronously, so wait for it
|
||||
expect(fetch).toHaveBeenCalledTimes(2)
|
||||
expect(fetch).toHaveBeenLastCalledWith(ALL_EMOJI, { method: 'HEAD' })
|
||||
await db.delete()
|
||||
})
|
||||
|
||||
test('calls GET first and tries HEAD if ETag unavailable', async () => {
|
||||
let db = new Database({ dataSource: ALL_EMOJI_NO_ETAG })
|
||||
await db.ready()
|
||||
expect(fetch).toHaveBeenCalledTimes(1)
|
||||
expect(fetch).toHaveBeenLastCalledWith(ALL_EMOJI_NO_ETAG, undefined)
|
||||
await db.close()
|
||||
db = new Database({ dataSource: ALL_EMOJI_NO_ETAG })
|
||||
await db.ready()
|
||||
await new Promise(resolve => setTimeout(resolve)) // the request is done asynchronously, so wait for it
|
||||
expect(fetch).toHaveBeenCalledTimes(3)
|
||||
expect(fetch).toHaveBeenNthCalledWith(2, ALL_EMOJI_NO_ETAG, { method: 'HEAD' })
|
||||
expect(fetch).toHaveBeenLastCalledWith(ALL_EMOJI_NO_ETAG, undefined)
|
||||
await db.delete()
|
||||
})
|
||||
|
||||
test('database deletion actually deletes and causes re-fetch', async () => {
|
||||
let db = new Database({ dataSource: ALL_EMOJI })
|
||||
await db.ready()
|
||||
expect(fetch).toHaveBeenCalledTimes(1)
|
||||
expect(fetch).toHaveBeenLastCalledWith(ALL_EMOJI, undefined)
|
||||
await db.delete()
|
||||
db = new Database({ dataSource: ALL_EMOJI })
|
||||
await db.ready()
|
||||
expect(fetch).toHaveBeenCalledTimes(2)
|
||||
expect(fetch).toHaveBeenLastCalledWith(ALL_EMOJI, undefined)
|
||||
await db.delete()
|
||||
})
|
||||
|
||||
test('misconfigured server where ETag in GET but not HEAD still works', async () => {
|
||||
let db = new Database({ dataSource: ALL_EMOJI_MISCONFIGURED_ETAG })
|
||||
await db.ready()
|
||||
expect(fetch).toHaveBeenCalledTimes(1)
|
||||
expect(fetch).toHaveBeenLastCalledWith(ALL_EMOJI_MISCONFIGURED_ETAG, undefined)
|
||||
await db.close()
|
||||
db = new Database({ dataSource: ALL_EMOJI_MISCONFIGURED_ETAG })
|
||||
await db.ready()
|
||||
await new Promise(resolve => setTimeout(resolve)) // the request is done asynchronously, so wait for it
|
||||
expect(fetch).toHaveBeenCalledTimes(3)
|
||||
expect(fetch).toHaveBeenNthCalledWith(2, ALL_EMOJI_MISCONFIGURED_ETAG, { method: 'HEAD' })
|
||||
expect(fetch).toHaveBeenLastCalledWith(ALL_EMOJI_MISCONFIGURED_ETAG, undefined)
|
||||
await db.delete()
|
||||
})
|
||||
})
|
||||
|
|
75
yarn.lock
75
yarn.lock
|
@ -1050,6 +1050,34 @@
|
|||
"@nodelib/fs.scandir" "2.1.3"
|
||||
fastq "^1.6.0"
|
||||
|
||||
"@peculiar/asn1-schema@^2.0.1", "@peculiar/asn1-schema@^2.0.3":
|
||||
version "2.0.5"
|
||||
resolved "https://registry.yarnpkg.com/@peculiar/asn1-schema/-/asn1-schema-2.0.5.tgz#ba6c5a107eec16a23804d0176a3595837b53c0e9"
|
||||
integrity sha512-VIKJjsgMkv+yyWx3C+D4xo6/NeCg0XFBgNlavtkxELijV+aKAq53du5KkOJbeZtm1nn9CinQKny2PqL8zCfpeA==
|
||||
dependencies:
|
||||
"@types/asn1js" "^0.0.1"
|
||||
asn1js "^2.0.26"
|
||||
pvtsutils "^1.0.10"
|
||||
tslib "^1.11.1"
|
||||
|
||||
"@peculiar/json-schema@^1.1.10":
|
||||
version "1.1.10"
|
||||
resolved "https://registry.yarnpkg.com/@peculiar/json-schema/-/json-schema-1.1.10.tgz#d772b4323c9a4b5352b5ad52dc821a07b0db4877"
|
||||
integrity sha512-kbpnG9CkF1y6wwGkW7YtSA+yYK4X5uk4rAwsd1hxiaYE3Hkw2EsGlbGh/COkMLyFf+Fe830BoFiMSB3QnC/ItA==
|
||||
dependencies:
|
||||
tslib "^1.11.1"
|
||||
|
||||
"@peculiar/webcrypto@^1.1.1":
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@peculiar/webcrypto/-/webcrypto-1.1.1.tgz#4c7498e4861878e299ef058bce1208a4d063d0ff"
|
||||
integrity sha512-Bu2XgOvzirnLcojZYs4KQ8hOLf2ETpa0NL6btQt5NgsAwctI6yVkzgYP+EcG7Mm579RBP+V0LM5rXyMlTVx23A==
|
||||
dependencies:
|
||||
"@peculiar/asn1-schema" "^2.0.3"
|
||||
"@peculiar/json-schema" "^1.1.10"
|
||||
pvtsutils "^1.0.10"
|
||||
tslib "^1.11.2"
|
||||
webcrypto-core "^1.1.0"
|
||||
|
||||
"@rollup/plugin-commonjs@^11.1.0":
|
||||
version "11.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-11.1.0.tgz#60636c7a722f54b41e419e1709df05c7234557ef"
|
||||
|
@ -1149,6 +1177,13 @@
|
|||
lodash "^4.17.15"
|
||||
redent "^3.0.0"
|
||||
|
||||
"@types/asn1js@^0.0.1":
|
||||
version "0.0.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/asn1js/-/asn1js-0.0.1.tgz#ef8b9f9708cb1632a1c3a9cd27717caabe793bc2"
|
||||
integrity sha1-74uflwjLFjKhw6nNJ3F8qr55O8I=
|
||||
dependencies:
|
||||
"@types/pvutils" "*"
|
||||
|
||||
"@types/babel__core@^7.1.7":
|
||||
version "7.1.8"
|
||||
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.8.tgz#057f725aca3641f49fc11c7a87a9de5ec588a5d7"
|
||||
|
@ -1257,6 +1292,11 @@
|
|||
resolved "https://registry.yarnpkg.com/@types/pug/-/pug-2.0.4.tgz#8772fcd0418e3cd2cc171555d73007415051f4b2"
|
||||
integrity sha1-h3L80EGOPNLMFxVV1zAHQVBR9LI=
|
||||
|
||||
"@types/pvutils@*":
|
||||
version "0.0.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/pvutils/-/pvutils-0.0.2.tgz#e21684962cfa58ac920fd576d90556032dc86009"
|
||||
integrity sha512-CgQAm7pjyeF3Gnv78ty4RBVIfluB+Td+2DR8iPaU0prF18pkzptHHP+DoKPfpsJYknKsVZyVsJEu5AuGgAqQ5w==
|
||||
|
||||
"@types/resolve@0.0.8":
|
||||
version "0.0.8"
|
||||
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194"
|
||||
|
@ -1480,6 +1520,13 @@ asn1@~0.2.3:
|
|||
dependencies:
|
||||
safer-buffer "~2.1.0"
|
||||
|
||||
asn1js@^2.0.26:
|
||||
version "2.0.26"
|
||||
resolved "https://registry.yarnpkg.com/asn1js/-/asn1js-2.0.26.tgz#0a6d435000f556a96c6012969d9704d981b71251"
|
||||
integrity sha512-yG89F0j9B4B0MKIcFyWWxnpZPLaNTjCj4tkE3fjbAoo0qmpGw0PYYqSbX/4ebnd9Icn8ZgK4K1fvDyEtW1JYtQ==
|
||||
dependencies:
|
||||
pvutils latest
|
||||
|
||||
assert-plus@1.0.0, assert-plus@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525"
|
||||
|
@ -5653,6 +5700,18 @@ punycode@^2.1.0, punycode@^2.1.1:
|
|||
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
|
||||
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
|
||||
|
||||
pvtsutils@^1.0.10:
|
||||
version "1.0.10"
|
||||
resolved "https://registry.yarnpkg.com/pvtsutils/-/pvtsutils-1.0.10.tgz#157d0fcb853f570d32e0f8788179f3057eacdf38"
|
||||
integrity sha512-8ZKQcxnZKTn+fpDh7wL4yKax5fdl3UJzT8Jv49djZpB/dzPxacyN1Sez90b6YLdOmvIr9vaySJ5gw4aUA1EdSw==
|
||||
dependencies:
|
||||
tslib "^1.10.0"
|
||||
|
||||
pvutils@latest:
|
||||
version "1.0.17"
|
||||
resolved "https://registry.yarnpkg.com/pvutils/-/pvutils-1.0.17.tgz#ade3c74dfe7178944fe44806626bd2e249d996bf"
|
||||
integrity sha512-wLHYUQxWaXVQvKnwIDWFVKDJku9XDCvyhhxoq8dc5MFdIlRenyPI9eSfEtcvgHgD7FlvCyGAlWgOzRnZD99GZQ==
|
||||
|
||||
qs@6.7.0:
|
||||
version "6.7.0"
|
||||
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
|
||||
|
@ -6979,6 +7038,11 @@ trough@^1.0.0:
|
|||
resolved "https://registry.yarnpkg.com/trough/-/trough-1.0.5.tgz#b8b639cefad7d0bb2abd37d433ff8293efa5f406"
|
||||
integrity sha512-rvuRbTarPXmMb79SmzEp8aqXNKcK+y0XaB298IXueQ8I2PsrATcPBCSPyK/dDNa2iWOhKlfNnOjdAOTBU/nkFA==
|
||||
|
||||
tslib@^1.10.0, tslib@^1.11.1, tslib@^1.11.2:
|
||||
version "1.13.0"
|
||||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043"
|
||||
integrity sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==
|
||||
|
||||
tslib@^1.9.0:
|
||||
version "1.11.2"
|
||||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.11.2.tgz#9c79d83272c9a7aaf166f73915c9667ecdde3cc9"
|
||||
|
@ -7311,6 +7375,17 @@ wcwidth@^1.0.1:
|
|||
dependencies:
|
||||
defaults "^1.0.3"
|
||||
|
||||
webcrypto-core@^1.1.0:
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/webcrypto-core/-/webcrypto-core-1.1.1.tgz#c9cd26f8dea696d7b5f5c1b0598ff16e6bdcab7c"
|
||||
integrity sha512-xK61sFRUyZdSAJG7+bJox36+Tnhxw1PaMbmrLLp30HNTJ4mffqsY2jUMlmGq6OOoej3WO/SsH5serzlzBMZ+jg==
|
||||
dependencies:
|
||||
"@peculiar/asn1-schema" "^2.0.1"
|
||||
"@peculiar/json-schema" "^1.1.10"
|
||||
asn1js "^2.0.26"
|
||||
pvtsutils "^1.0.10"
|
||||
tslib "^1.11.2"
|
||||
|
||||
webidl-conversions@^4.0.2:
|
||||
version "4.0.2"
|
||||
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"
|
||||
|
|
Loading…
Reference in New Issue