2023-10-07 21:55:20 +02:00
|
|
|
import '@testing-library/jest-dom/jest-globals'
|
2023-11-11 17:12:20 +01:00
|
|
|
import { jest } from '@jest/globals'
|
2023-06-19 01:08:12 +02:00
|
|
|
import * as FakeIndexedDB from 'fake-indexeddb'
|
2020-06-05 16:50:40 +02:00
|
|
|
import { Crypto } from '@peculiar/webcrypto'
|
2020-12-27 18:54:37 +01:00
|
|
|
import { ResizeObserver } from 'd2l-resize-aware/resize-observer-module.js'
|
2021-06-29 05:31:44 +02:00
|
|
|
import { deleteDatabase } from '../src/database/databaseLifecycle'
|
2021-08-14 03:09:40 +02:00
|
|
|
import styles from '../node_modules/.cache/emoji-picker-element/styles.js'
|
2023-11-11 17:12:20 +01:00
|
|
|
import * as fetchMockJest from 'fetch-mock-jest'
|
2020-06-07 10:10:43 +02:00
|
|
|
|
2023-06-19 01:08:12 +02:00
|
|
|
const { IDBFactory, IDBKeyRange } = FakeIndexedDB
|
|
|
|
|
2023-01-22 19:05:56 +01:00
|
|
|
// See https://github.com/jsdom/jsdom/issues/3455#issuecomment-1333567714
|
2023-11-11 17:12:20 +01:00
|
|
|
globalThis.crypto.subtle = new Crypto().subtle
|
2023-01-22 19:05:56 +01:00
|
|
|
|
2023-11-11 17:12:20 +01:00
|
|
|
if (!globalThis.performance) {
|
|
|
|
globalThis.performance = {}
|
2021-05-31 17:45:59 +02:00
|
|
|
}
|
2023-11-11 17:12:20 +01:00
|
|
|
if (!globalThis.performance.mark) {
|
|
|
|
globalThis.performance.mark = () => {}
|
2021-05-31 17:45:59 +02:00
|
|
|
}
|
2023-11-11 17:12:20 +01:00
|
|
|
if (!globalThis.performance.measure) {
|
|
|
|
globalThis.performance.measure = () => {}
|
2021-05-31 17:45:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 04:05:03 +02:00
|
|
|
jest.setTimeout(60000)
|
2020-06-04 07:11:26 +02:00
|
|
|
|
2023-11-11 17:12:20 +01:00
|
|
|
globalThis.ResizeObserver = ResizeObserver
|
2020-06-07 10:10:43 +02:00
|
|
|
|
|
|
|
process.env.NODE_ENV = 'test'
|
2020-07-03 00:14:17 +02:00
|
|
|
|
2021-08-14 03:09:40 +02:00
|
|
|
process.env.STYLES = styles
|
|
|
|
|
2023-11-11 17:12:20 +01:00
|
|
|
globalThis.IDBKeyRange = IDBKeyRange
|
|
|
|
globalThis.indexedDB = new IDBFactory()
|
2021-05-31 17:45:59 +02:00
|
|
|
|
2023-11-11 18:06:31 +01:00
|
|
|
// Hack to work around an issue with jest-environment-jsdom https://github.com/jsdom/jsdom/issues/3363
|
|
|
|
globalThis.structuredClone = globalThis.structuredClone ?? (_ => JSON.parse(JSON.stringify(_)))
|
|
|
|
|
2021-05-31 17:45:59 +02:00
|
|
|
beforeAll(() => {
|
2023-11-11 17:12:20 +01:00
|
|
|
jest.spyOn(globalThis.console, 'log').mockImplementation()
|
|
|
|
jest.spyOn(globalThis.console, 'warn').mockImplementation()
|
|
|
|
|
|
|
|
const fetch = fetchMockJest.default.sandbox()
|
|
|
|
globalThis.fetch = fetch
|
|
|
|
globalThis.Response = fetch.Response
|
2021-05-31 17:45:59 +02:00
|
|
|
})
|
|
|
|
|
2021-06-29 05:31:44 +02:00
|
|
|
afterEach(async () => {
|
|
|
|
// fresh indexedDB for every test
|
2023-11-11 17:12:20 +01:00
|
|
|
const dbs = await globalThis.indexedDB.databases()
|
2021-06-29 05:31:44 +02:00
|
|
|
await Promise.all(dbs.map(({ name }) => deleteDatabase(name)))
|
2020-07-03 00:14:17 +02:00
|
|
|
})
|