Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed Nov 25, 2017
1 parent dd7d034 commit a6ca81d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
},
"jest": {
"coverageReporters": [
"text",
"lcov"
]
}
Expand Down
40 changes: 30 additions & 10 deletions src/cache/__tests__/FifoCacheObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@ describe('FifoCacheObject', () => {
expect(actual).toBe(expected);
});

it('Should limit cache queue by removing the first item', () => {
it('Should limit cache queue by removing the first added items', () => {
const cache = newCache(5);
const entries = [1, 2, 3, 4];
const newEntries = [5, 6, 7];

cache.set(0, 0);
const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, entries);
fillCache(cache, newEntries);

expect(cache.get(0)).toBe(undefined);
newEntries.map(entry => {
expect(cache.get(1)).toBe(undefined);
expect(cache.get(2)).toBe(undefined);
[4, 5, 6, 7].map(entry => {
expect(cache.get(entry)).toBe(entry);
});
});

it('Should remove a single item', () => {
const cache = newCache(5);
const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, newEntries);
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

cache.remove(3);

Expand All @@ -46,14 +48,32 @@ describe('FifoCacheObject', () => {
});
});

it('Should mantain cache updated after removing extraneous entry', () => {
const cache = newCache(5);
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

cache.remove(7); // Extraneous
cache.remove(3);
cache.set(6, 6);
cache.set(7, 7);

expect(cache.get(1)).toBe(undefined);
expect(cache.get(3)).toBe(undefined);

[2, 4, 5, 6, 7].map(entry => {
expect(cache.get(entry)).toBe(entry);
});
});

it('Should clear the cache', () => {
const cache = newCache(5);
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, newEntries);
cache.clear();

newEntries.map(entry => {
[1, 2, 3, 4, 5].map(entry => {
expect(cache.get(entry)).toBe(undefined);
});
});
Expand Down
10 changes: 5 additions & 5 deletions src/cache/__tests__/FlatCacheObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('FlatCacheObject', () => {

it('Should remove a single item', () => {
const cache = new FlatCacheObject();
const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, newEntries);
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

cache.remove(3);

Expand All @@ -31,12 +31,12 @@ describe('FlatCacheObject', () => {

it('Should clear the cache', () => {
const cache = new FlatCacheObject();
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, newEntries);
cache.clear();

newEntries.map(entry => {
[1, 2, 3, 4, 5].map(entry => {
expect(cache.get(entry)).toBe(undefined);
});
});
Expand Down
24 changes: 12 additions & 12 deletions src/cache/__tests__/LruCacheObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('LruCacheObject', () => {

it('Should remove a single item', () => {
const cache = newCache(5);
const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, newEntries);
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

cache.remove(3);

Expand All @@ -32,8 +32,8 @@ describe('LruCacheObject', () => {

it('Should remove a single item und update cache ordering', () => {
const cache = newCache(5);
const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, newEntries);
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

cache.remove(3);
cache.set(6, 6);
Expand All @@ -46,29 +46,29 @@ describe('LruCacheObject', () => {

it('Should clear the cache', () => {
const cache = newCache(5);
const newEntries = [1, 2, 3, 4, 5];
fillCache(cache, newEntries);
const entries = [1, 2, 3, 4, 5];
fillCache(cache, entries);

cache.clear();

newEntries.map(entry => {
[1, 2, 3, 4, 5].map(entry => {
expect(cache.get(entry)).toBe(undefined);
});
});

it('Should limit cache queue by removing the least recently used item', () => {
const cache = newCache(5);

const newEntries1 = [0, 1, 2];
const newEntries2 = [3, 4, 5];
fillCache(cache, newEntries1);
const entries = [0, 1, 2];
const newEntries = [3, 4, 5];
fillCache(cache, entries);
cache.get(0);
fillCache(cache, newEntries2);
fillCache(cache, newEntries);

expect(cache.get(0)).toBe(0);
expect(cache.get(1)).toBe(undefined);
expect(cache.get(2)).toBe(2);
newEntries2.map(entry => {
newEntries.map(entry => {
expect(cache.get(entry)).toBe(entry);
});
});
Expand Down

0 comments on commit a6ca81d

Please sign in to comment.