Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
test: fix localStorage mocking in Bun tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drichar committed Dec 9, 2023
1 parent 86952e9 commit 98f97fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 22 additions & 1 deletion __tests__/store/store.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import { Mock, beforeEach, describe, expect, it } from 'bun:test'
import { Mock, beforeEach, describe, expect, it, spyOn } from 'bun:test'
import { NetworkId } from 'src/network/constants'
import { StoreActions, StoreMutations, createStore, defaultState } from 'src/store'
import { LOCAL_STORAGE_KEY } from 'src/store/constants'
import { replacer } from 'src/store/utils'
import { WalletId } from 'src/wallets/supported/constants'

// Suppress console output
spyOn(console, 'info').mockImplementation(() => {})
spyOn(console, 'warn').mockImplementation(() => {})
spyOn(console, 'error').mockImplementation(() => {})
spyOn(console, 'groupCollapsed').mockImplementation(() => {})

// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, any> = {}
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: any) => (store[key] = value.toString()),
clear: () => (store = {})
}
})()
if (!localStorage) {
Object.defineProperty(global, 'localStorage', {
value: localStorageMock
})
}

describe('Store', () => {
beforeEach(() => {
localStorage.clear()
Expand Down
8 changes: 5 additions & 3 deletions __tests__/wallets/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ const localStorageMock = (() => {
clear: () => (store = {})
}
})()
Object.defineProperty(global, 'localStorage', {
value: localStorageMock
})
if (!localStorage) {
Object.defineProperty(global, 'localStorage', {
value: localStorageMock
})
}

const deflyResumeSession = spyOn(DeflyWallet.prototype, 'resumeSession').mockImplementation(() =>
Promise.resolve()
Expand Down

0 comments on commit 98f97fa

Please sign in to comment.