Skip to content

Commit

Permalink
Added test for ssr/forceFetch functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Jun 27, 2016
1 parent 81c0f60 commit aa7d9c3
Show file tree
Hide file tree
Showing 8 changed files with 1,019 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@
"pretty-bytes": "^3.0.1",
"remap-istanbul": "^0.5.1",
"request-promise": "^2.0.1",
"rxjs": "^5.0.0-beta.7",
"sinon": "^1.17.4",
"source-map-support": "^0.4.0",
"swapi-graphql": "^0.0.4",
"tslint": "3.7.3",
"typescript": "^1.8.9",
"typings": "^1.0.0",
"uglify-js": "^2.6.2",
"rxjs": "^5.0.0-beta.7"
"uglify-js": "^2.6.2"
}
}
100 changes: 100 additions & 0 deletions test/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as chai from 'chai';
const { assert } = chai;
import * as sinon from 'sinon';

import ApolloClient from '../src';

Expand Down Expand Up @@ -879,4 +880,103 @@ describe('client', () => {
});
});
});

describe.only('forceFetch', () => {
const query = gql`
query number {
myNumber {
n
}
}
`;

const firstFetch = {
myNumber: {
n: 1,
},
};
const secondFetch = {
myNumber: {
n: 2,
},
};


let networkInterface;
let clock;
beforeEach(() => {
networkInterface = mockNetworkInterface({
request: { query },
result: { data: firstFetch },
}, {
request: { query },
result: { data: secondFetch },
});
});

afterEach(() => {
if (clock) {
clock.reset();
}
});

it('forces the query to rerun', () => {
const client = new ApolloClient({
networkInterface,
});

// Run a query first to initialize the store
return client.query({ query })
// then query for real
.then(() => client.query({ query, forceFetch: true }))
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 2 } });
});
});

it('can be disabled with ssrMode', () => {
const client = new ApolloClient({
networkInterface,
ssrMode: true,
});

// Run a query first to initialize the store
return client.query({ query })
// then query for real
.then(() => client.query({ query, forceFetch: true }))
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 1 } });
});
});

it('can temporarily be disabled with ssrForceFetchDelay', () => {
clock = sinon.useFakeTimers();

const client = new ApolloClient({
networkInterface,
ssrForceFetchDelay: 100,
});

// Run a query first to initialize the store
const outerPromise = client.query({ query })
// then query for real
.then(() => {
const promise = client.query({ query, forceFetch: true });
clock.tick(0);
return promise;
})
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 1 } });
clock.tick(100);
const promise = client.query({ query, forceFetch: true });
clock.tick(0);
return promise;
})
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 2 } });
});
clock.tick(0);
return outerPromise;
});
});
});
1 change: 1 addition & 0 deletions typings/browser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
/// <reference path="globals/promises-a-plus/index.d.ts" />
/// <reference path="globals/redux/index.d.ts" />
/// <reference path="modules/lodash/index.d.ts" />
/// <reference path="modules/sinon/index.d.ts" />
Loading

0 comments on commit aa7d9c3

Please sign in to comment.