Skip to content

Commit

Permalink
tests: fixed memory storage clone test
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Mar 11, 2022
1 parent 08d7ab4 commit a0e05e8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
6 changes: 0 additions & 6 deletions test/storage/common.test.ts

This file was deleted.

5 changes: 5 additions & 0 deletions test/storage/is-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/** @jest-environment jsdom */

import { Axios } from 'axios';
import { isStorage } from '../../src/storage/build';
import { buildMemoryStorage } from '../../src/storage/memory';
import type { AxiosStorage } from '../../src/storage/types';
import { buildWebStorage } from '../../src/storage/web-api';
import { mockAxios } from '../mocks/axios';

it('tests isStorage function', () => {
Expand All @@ -16,6 +19,8 @@ it('tests isStorage function', () => {
expect(isStorage({ a: 1, b: 'a' })).toBe(false);

expect(isStorage(buildMemoryStorage())).toBe(true);
expect(isStorage(buildWebStorage(localStorage))).toBe(true);
expect(isStorage(buildWebStorage(sessionStorage))).toBe(true);
});

it('tests setupCache without proper storage', () => {
Expand Down
39 changes: 39 additions & 0 deletions test/storage/memory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { buildMemoryStorage } from '../../src/storage/memory';
import type { CachedStorageValue } from '../../src/storage/types';
import { EMPTY_RESPONSE } from '../utils';
import { testStorage } from './storages';

describe('tests memory storage', () => {
testStorage('memory', () => buildMemoryStorage());

// Expects that when a result returned by storage.get() has his inner properties updated,
// a new request to storage.get() should maintain the same value.
//
// https://github.com/arthurfiorette/axios-cache-interceptor/issues/163
it('not allow changes by value reference', async () => {
const storage = buildMemoryStorage(true);

await storage.set('key', {
state: 'cached',
createdAt: Date.now(),
ttl: 1000 * 60 * 5, // 5 Minutes
data: { ...EMPTY_RESPONSE, data: 'data' }
});

const result = (await storage.get('key')) as CachedStorageValue;

expect(result).not.toBeNull();
expect(result.state).toBe('cached');
expect(result.data.data).toBe('data');

// Deletes the value
delete result.data.data;

// Check if the value has been modified
const result2 = await storage.get('key');

expect(result2).not.toBeNull();
expect(result2.state).toBe('cached');
expect(result2.data?.data).toBe('data');
});
});
33 changes: 1 addition & 32 deletions test/storage/storages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AxiosStorage, CachedStorageValue } from '../../src/storage/types';
import type { AxiosStorage } from '../../src/storage/types';
import { EMPTY_RESPONSE } from '../utils';

export function testStorage(name: string, Storage: () => AxiosStorage): void {
Expand Down Expand Up @@ -58,35 +58,4 @@ export function testStorage(name: string, Storage: () => AxiosStorage): void {

jest.useRealTimers();
});

// Expects that when a result returned by storage.get() has his inner properties updated,
// a new request to storage.get() should maintain the same value.
//
// https://github.com/arthurfiorette/axios-cache-interceptor/issues/163
it(`tests ${name} to not allow changes by value reference`, async () => {
const storage = Storage();

await storage.set('key', {
state: 'cached',
createdAt: Date.now(),
ttl: 1000 * 60 * 5, // 5 Minutes
data: { ...EMPTY_RESPONSE, data: 'data' }
});

const result = (await storage.get('key')) as CachedStorageValue;

expect(result).not.toBeNull();
expect(result.state).toBe('cached');
expect(result.data.data).toBe('data');

// Deletes the value
delete result.data.data;

// Check if the value has been modified
const result2 = await storage.get('key');

expect(result2).not.toBeNull();
expect(result2.state).toBe('cached');
expect(result2.data?.data).toBe('data');
});
}
2 changes: 1 addition & 1 deletion test/util/key-generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CacheRequestConfig } from '../../src';
import type { CacheRequestConfig } from '../../src/cache/axios';
import { buildKeyGenerator, defaultKeyGenerator } from '../../src/util/key-generator';
import { mockAxios } from '../mocks/axios';

Expand Down

0 comments on commit a0e05e8

Please sign in to comment.