Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
fix(cache): handle empty resource lists
Browse files Browse the repository at this point in the history
  • Loading branch information
moranje committed Feb 16, 2020
1 parent cd090d8 commit af89785
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
43 changes: 37 additions & 6 deletions src/lib/todoist/local-rest-adapter/conf-cache/tests/conf-cache.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
/* eslint-disable jest/no-hooks */
jest.mock('@/lib/todoist/local-rest-adapter/conf-cache');

import { CacheStore } from '@/lib/todoist/local-rest-adapter/conf-cache';
import { LOCAL_FIXTURES } from '@/tests/helpers/fixtures';
import fs from 'fs';
import { spyOnImplementing } from 'jest-mock-process';

// TODO: mock fs.writeWileSync()
// jest.mock('write-file-atomic', (path: string, data: any) => {});
// let fsMock = spyOnImplementing(fs, 'writeFileSync', (...args: any) => args);

let store;
let store = new CacheStore({
cwd: 'src/tests/stores',
configName: 'cache',
cacheTimeout: 100,
});

describe('unit: Cache timestamp store', () => {
beforeEach(() => {
store = new CacheStore({});
store.reset();
store.set('TASK', LOCAL_FIXTURES.tasks);
});

it('.get()', () => {
expect.assertions(2);

expect(store.get('<missing>')).toBeUndefined();
expect(store.get('TASK')).toEqual(LOCAL_FIXTURES.tasks);
});

it.todo('should test something');
it('.set()', () => {
expect.assertions(2);
store.set('ITEM', 'Item');
let cache = store.get('_cache');

expect(store.get('ITEM')).toBe('Item');
expect(cache).toBe(3);
});

it.todo('.has()');

it.todo('.reset()');

it.todo('.isExipred()');

// afterAll(() => {
// fsMock.mockRestore();
// });
});
4 changes: 2 additions & 2 deletions src/lib/todoist/local-rest-adapter/local-rest-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class LocalRESTAdapter<
* @returns The cached resource.
*/
peekAll(): Store[Name] {
return this.store.get(this.type);
return this.store.get(this.type) ?? [];
}

/**
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class LocalRESTAdapter<
async findAll(options?: any): Promise<GetResourceByName<Name>[]> {
const cache = this.peekAll() as GetResourceByName<Name>[];

if (cache != null && !options?.skipCache) return cache;
if (cache.length > 0 && !options?.skipCache) return cache;

const remote = await super.findAll(options);
if (!equal(remote, this.store.get(this.type))) {
Expand Down

0 comments on commit af89785

Please sign in to comment.