From d24bdcf7d1b497cbdd9f05a25fee87ef8f0ef043 Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Wed, 8 May 2024 08:04:07 -0400 Subject: [PATCH] tests: Add tests for storageInit() Ensures that default settings are being merged with the existing extension settings found in the browser as appropriate. --- tests/lib/storageInit.test.ts | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/lib/storageInit.test.ts diff --git a/tests/lib/storageInit.test.ts b/tests/lib/storageInit.test.ts new file mode 100644 index 0000000..9c9bd55 --- /dev/null +++ b/tests/lib/storageInit.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it, vi } from 'vitest' +import { storageInit } from '@/lib/storageInit' +import type { StorageAll } from '@/types/StorageAll' + +const browserMock = { + storage: { + sync: { + get: vi.fn(async (_: string | string[] | Record | null | undefined): Promise> => ({ blockDefault: true })), + set: vi.fn(async (_: Record): Promise => {}) + }, + + local: { + get: vi.fn(async (_: string | string[] | Record | null | undefined): Promise> => ({})), + set: vi.fn(async (_: Record): Promise => {}) + } + } +} + +vi.stubGlobal('browser', browserMock) + +const defaultStorage: StorageAll = { + sync: { + provider: 'mullvad', + blockDefault: false + }, + + local: { + servers: [], + lastUpdated: 0 + } +} + +describe('storageInit()', () => { + it('should initialize with default values with existing browser storage values taking priority', async () => { + expect(await storageInit(defaultStorage)).toEqual({ + sync: { + provider: 'mullvad', + blockDefault: true + }, + + local: { + servers: [], + lastUpdated: 0 + } + }) + }) +})