Skip to content

Commit

Permalink
Add tests for caching in getNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Dec 29, 2017
1 parent fee9e16 commit 00776ce
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/gitnews.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* global describe, it */
const chai = require( 'chai' );
const chaiSubset = require( 'chai-subset' );
const sinon = require( 'sinon' );
const sinonChai = require( 'sinon-chai' );
const { createNoteGetter } = require( '../index' );
const {
isError,
Expand All @@ -9,6 +11,7 @@ const {
} = require( './helpers.js' );

chai.use( chaiSubset );
chai.use( sinonChai );
const { expect } = chai;
const noop = () => {};

Expand Down Expand Up @@ -138,6 +141,41 @@ describe( 'gitnews', function() {
} );
} );

it( 'calls the fetch function once for each url if caching is disabled', function() {
const fetch = sinon.stub().callsFake( getMockFetchForPatterns( {
notification: { json: [
{ id: 5, subject: { url: 'subjectUrl' } },
{ id: 6, subject: { url: 'subjectUrl' } },
] },
subjectUrl: { json: { html_url: 'htmlUrl' } }, // eslint-disable-line camelcase
} ) );
const getNotifications = createNoteGetter( { fetch, getCachedResponseFor: noop } );
return getNotifications( '123abc' )
.then( () => {
expect( fetch ).to.have.callCount( 5 );
} );
} );

it( 'does not call the fetch function for a url that has already been fetched if caching is enabled', function() {
const fetch = sinon.stub().callsFake( getMockFetchForPatterns( {
notification: { json: [
{ id: 5, subject: { url: 'subjectUrl' } },
{ id: 6, subject: { url: 'subjectUrl' } },
] },
subjectUrl: { json: { html_url: 'htmlUrl' } }, // eslint-disable-line camelcase
} ) );
const getNotifications = createNoteGetter( { fetch } );
return getNotifications( '123abc' )
.then( () => {
// TODO: the cache fails for the subject calls because the second
// call to fetch is triggered before the first call completes
// (Promise.all), so the cache misses on the second call. It succeeds
// for the comment fetches because they only happen after the subject
// fetches are all complete.
expect( fetch ).to.have.callCount( 3 );
} );
} );

describe( 'each notification', function() {
it( 'has a unique id even when data is invalid', function() {
const fetch = getMockFetch( [ {}, {} ] );
Expand Down

0 comments on commit 00776ce

Please sign in to comment.