Skip to content

Commit

Permalink
fix: Config utils test beforeEach and afterEach scopes
Browse files Browse the repository at this point in the history
Aldemylla committed Nov 29, 2024
1 parent 1fca98e commit 0817a9f
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions src/utils/__tests__/config.spec.js
Original file line number Diff line number Diff line change
@@ -10,40 +10,40 @@ import {
let mockSessionStorage;
let mockLocalStorage;

beforeEach(() => {
mockSessionStorage = {};
mockLocalStorage = {};

vi.stubGlobal('sessionStorage', {
getItem: vi.fn((key) => mockSessionStorage[key] || null),
setItem: vi.fn((key, value) => {
mockSessionStorage[key] = value;
}),
clear: vi.fn(() => {
Object.keys(mockSessionStorage).forEach(
(key) => delete mockSessionStorage[key],
);
}),
});
describe('Config storage', () => {
beforeEach(() => {
mockSessionStorage = {};
mockLocalStorage = {};

vi.stubGlobal('sessionStorage', {
getItem: vi.fn((key) => mockSessionStorage[key] || null),
setItem: vi.fn((key, value) => {
mockSessionStorage[key] = value;
}),
clear: vi.fn(() => {
Object.keys(mockSessionStorage).forEach(
(key) => delete mockSessionStorage[key],
);
}),
});

vi.stubGlobal('localStorage', {
getItem: vi.fn((key) => mockLocalStorage[key] || null),
setItem: vi.fn((key, value) => {
mockLocalStorage[key] = value;
}),
clear: vi.fn(() => {
Object.keys(mockLocalStorage).forEach(
(key) => delete mockLocalStorage[key],
);
}),
vi.stubGlobal('localStorage', {
getItem: vi.fn((key) => mockLocalStorage[key] || null),
setItem: vi.fn((key, value) => {
mockLocalStorage[key] = value;
}),
clear: vi.fn(() => {
Object.keys(mockLocalStorage).forEach(
(key) => delete mockLocalStorage[key],
);
}),
});
});
});

afterEach(() => {
vi.restoreAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});

describe('Config storage', () => {
describe('Token', () => {
it('should get the token from sessionStorage first, then localStorage', () => {
mockSessionStorage['WENICHATS_API_TOKEN'] = 'session-token';
@@ -93,6 +93,7 @@ describe('Config storage', () => {
expect(mockLocalStorage['WENICHATS_API_TOKEN']).toBe(token);
});
});

describe('Project', () => {
it('should get the project from sessionStorage first, then localStorage', () => {
mockSessionStorage['WENICHATS_PROJECT_UUID'] = 'session-project';

0 comments on commit 0817a9f

Please sign in to comment.