Skip to content

Commit

Permalink
test: check what happens with cache when async storage rejects
Browse files Browse the repository at this point in the history
  • Loading branch information
kidroca committed May 27, 2021
1 parent ceb4acf commit 61c0c82
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function get(key) {
const parsed = JSON.parse(val);
cache.update(key, parsed);
return parsed;
})
.catch(err => logInfo(`Unable to get item from persistent storage. Key: ${key} Error: ${err}`)));
}))
.catch(err => logInfo(`Unable to get item from persistent storage. Key: ${key} Error: ${err}`));
}

/**
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/onyxCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('Onyx', () => {
// WHEN a value is retrieved
const result = await cache.getValue('mockKey', mockFallback);

// THEN it should be undefined
// THEN the fallback should be used to retrieve the value
expect(result).toEqual('myResult');
expect(mockFallback).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -146,6 +146,20 @@ describe('Onyx', () => {
expect(results[3]).toEqual('Result for mockKey1');
expect(results[4]).toEqual('Result for mockKey2');
});

it('Should not store cache if the fallback method failed', async () => {
// GIVEN empty cache and a fallback function that rejects
const mockFallback = jest.fn().mockRejectedValue(
new Error('Unable to get item from persistent storage')
);

// WHEN a value is retrieved
return cache.getValue('mockKey', mockFallback)
.catch(() => {
// THEN no value should be in cache
expect(cache.hasCacheForKey('mockKey')).toBe(false);
});
});
});

describe('hasCacheForKey', () => {
Expand Down

0 comments on commit 61c0c82

Please sign in to comment.