Expose `has` method on Cache.

Reviewed By: davidaurelio

Differential Revision: D2862315

fb-gh-sync-id: 03728a2593b477aef3bbfe01d42382893a05ea50
This commit is contained in:
Christoph Pojer 2016-01-26 10:50:19 -08:00 committed by facebook-github-bot-5
parent 7f5f8d10b5
commit 2049eb99fd
2 changed files with 31 additions and 3 deletions

View File

@ -275,4 +275,32 @@ describe('Cache', () => {
expect(fs.writeFile).toBeCalled();
});
});
describe('check for cache presence', () => {
it('synchronously resolves cache presence', () => {
fs.stat.mockImpl((file, callback) =>
callback(null, {
mtime: {
getTime: () => {},
},
})
);
var cache = new Cache({
cacheKey: 'cache',
});
var loaderCb = jest.genMockFn().mockImpl(() =>
Promise.resolve('banana')
);
var file = '/rootDir/someFile';
return cache
.get(file, 'field', loaderCb)
.then(() => {
expect(cache.has(file)).toBe(true);
expect(cache.has(file, 'field')).toBe(true);
expect(cache.has(file, 'foo')).toBe(false);
});
});
});
});

View File

@ -51,7 +51,7 @@ class Cache {
throw new Error('Use absolute paths');
}
var recordP = this._has(filepath, field)
var recordP = this.has(filepath, field)
? this._data[filepath].data[field]
: this._set(filepath, field, loaderCb(filepath));
@ -59,7 +59,7 @@ class Cache {
}
invalidate(filepath) {
if (this._has(filepath)) {
if (this.has(filepath)) {
delete this._data[filepath];
}
}
@ -68,7 +68,7 @@ class Cache {
return this._persistCache();
}
_has(filepath, field) {
has(filepath, field) {
return Object.prototype.hasOwnProperty.call(this._data, filepath) &&
(!field || Object.prototype.hasOwnProperty.call(this._data[filepath].data, field));
}