Skip to content

Commit

Permalink
feat!: sdk version 6
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- cache now returning plain objects instead of maps
- renamed and updated getRequiredRecords
- onNsUpdate removed in favor of `update` event
- useSuspense in rect TolgeeProvider is now off by default
- TolgeeProvider for vue has now standardized API
  • Loading branch information
stepan662 committed Dec 31, 2024
1 parent b33f6c8 commit f324185
Show file tree
Hide file tree
Showing 68 changed files with 1,421 additions and 1,073 deletions.
188 changes: 188 additions & 0 deletions packages/core/src/Controller/Cache/Cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { ListenerEvent } from '../../types';
import { EventEmitterInstance } from '../Events/EventEmitter';
import { ValueObserver } from '../ValueObserver';
import { Cache, CacheInstance } from './Cache';

describe('cache', () => {
function createEventMock(): EventEmitterInstance<ListenerEvent<any, any>> {
return {
emit: jest.fn(),
listen: jest.fn(),
};
}

let mockedBackendGetRecord: jest.Mock<any, any>;
let mockedBackendGetDevRecord: jest.Mock<any, any>;

let onFetchingChange: jest.Mock<any, any>;
let onLoadingChange: jest.Mock<any, any>;

let cache: CacheInstance;

beforeEach(() => {
const mockedEvents = {
on: jest.fn(),
onCacheChange: createEventMock(),
onError: createEventMock(),
onFetchingChange: createEventMock(),
onInitialLoaded: createEventMock(),
onLanguageChange: createEventMock(),
onLoadingChange: createEventMock(),
onPendingLanguageChange: createEventMock(),
onPermanentChange: createEventMock(),
onRunningChange: createEventMock(),
onUpdate: createEventMock() as any,
setEmitterActive: jest.fn(),
};

mockedBackendGetRecord = jest.fn((args) => {
return Promise.resolve({ data: 'Prod', ...args });
});
mockedBackendGetDevRecord = jest.fn((args) => {
return Promise.resolve({ data: 'Dev', ...args });
});

onFetchingChange = jest.fn();
onLoadingChange = jest.fn();

const fetchingObserver = ValueObserver(
false,
() => cache.isFetching(),
onFetchingChange
);

const loadingObserver = ValueObserver(
false,
() => cache.isLoading('en'),
onLoadingChange
);

cache = Cache(
mockedEvents,
mockedBackendGetRecord,
mockedBackendGetDevRecord,
(descriptor) => ({ namespace: '', ...descriptor }),
() => false,
fetchingObserver,
loadingObserver
);
});

it('fetches language with default namespace', async () => {
const result = await cache.loadRecords([{ language: 'en' }]);
expect(result[0].data).toEqual({
language: 'en',
namespace: '',
data: 'Dev',
});
});

it('fetches language with specified namespace', async () => {
const result = await cache.loadRecords([
{ language: 'en', namespace: 'test' },
]);
expect(result[0].data).toEqual({
language: 'en',
namespace: 'test',
data: 'Dev',
});
});

it('fetches language with specified namespace', async () => {
const result = await cache.loadRecords([
{ language: 'en', namespace: 'test' },
]);
expect(result[0].data).toEqual({
language: 'en',
namespace: 'test',
data: 'Dev',
});
});

it('uses cache when fetching twice the same thing', async () => {
await cache.loadRecords([{ language: 'en' }]);
expect(mockedBackendGetDevRecord).toBeCalledTimes(1);
const result = await cache.loadRecords([{ language: 'en' }], {
useCache: true,
});
expect(mockedBackendGetDevRecord).toBeCalledTimes(1);
expect(result[0].data).toEqual({
language: 'en',
namespace: '',
data: 'Dev',
});
});

it('uses cache when fetching twice production data', async () => {
await cache.loadRecords([{ language: 'en' }], { noDev: true });
expect(mockedBackendGetRecord).toBeCalledTimes(1);
const result = await cache.loadRecords([{ language: 'en' }], {
noDev: true,
useCache: true,
});
expect(mockedBackendGetRecord).toBeCalledTimes(1);
expect(result[0].data).toEqual({
language: 'en',
namespace: '',
data: 'Prod',
});
});

it('does not use cache when `noCache` is on', async () => {
await cache.loadRecords([{ language: 'en' }]);
expect(mockedBackendGetDevRecord).toBeCalledTimes(1);
await cache.loadRecords([{ language: 'en' }]);
expect(mockedBackendGetDevRecord).toBeCalledTimes(2);
});

it('correctly returns combination of non-cached and cached', async () => {
await cache.loadRecords([{ language: 'en' }]);
expect(mockedBackendGetDevRecord).toBeCalledTimes(1);
const result = await cache.loadRecords(
[{ language: 'en' }, { language: 'en', namespace: 'new' }],
{ useCache: true }
);
expect(mockedBackendGetDevRecord).toBeCalledTimes(2);
expect(result).toEqual([
{
cacheKey: 'en',
data: { data: 'Dev', language: 'en', namespace: '' },
language: 'en',
namespace: '',
},
{
cacheKey: 'en:new',
data: { data: 'Dev', language: 'en', namespace: 'new' },
language: 'en',
namespace: 'new',
},
]);
});

it('correctly refetches dev data', async () => {
await cache.loadRecords([{ language: 'en' }], { noDev: true });
expect(mockedBackendGetRecord).toBeCalledTimes(1);
expect(mockedBackendGetDevRecord).toBeCalledTimes(0);
cache.invalidate();
const result = await cache.loadRecords([{ language: 'en' }]);
expect(mockedBackendGetRecord).toBeCalledTimes(1);
expect(mockedBackendGetDevRecord).toBeCalledTimes(1);
expect(result[0].data).toEqual({
language: 'en',
namespace: '',
data: 'Dev',
});
});

it('correctly notifies about fetching and loading', async () => {
expect(onLoadingChange).toBeCalledTimes(0);
expect(onFetchingChange).toBeCalledTimes(0);
await cache.loadRecords([{ language: 'en' }]);
expect(onLoadingChange).toBeCalledTimes(2);
expect(onFetchingChange).toBeCalledTimes(2);
cache.invalidate();
await cache.loadRecords([{ language: 'en' }]);
expect(onFetchingChange).toBeCalledTimes(4);
expect(onLoadingChange).toBeCalledTimes(2);
});
});
Loading

0 comments on commit f324185

Please sign in to comment.