-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #554
- Loading branch information
Showing
7 changed files
with
181 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import * as chai from 'chai'; | ||
const { assert } = chai; | ||
|
||
import gql from 'graphql-tag'; | ||
|
||
import mockWatchQuery from './mocks/mockWatchQuery'; | ||
import { ObservableQuery } from '../src/ObservableQuery'; | ||
|
||
// I'm not sure why mocha doesn't provide something like this, you can't | ||
// always use promises | ||
const wrap = (done: Function, cb: (...args: any[]) => any) => (...args: any[]) => { | ||
try { | ||
return cb(...args); | ||
} catch (e) { | ||
done(e); | ||
} | ||
} | ||
|
||
describe('ObservableQuery', () => { | ||
describe('setVariables', () => { | ||
const query = gql` | ||
query($id: ID!){ | ||
people_one(id: $id) { | ||
name | ||
} | ||
} | ||
`; | ||
const variables = { id: 1 }; | ||
const differentVariables = { id: 2 }; | ||
const dataOne = { | ||
people_one: { | ||
name: 'Luke Skywalker', | ||
}, | ||
}; | ||
const dataTwo = { | ||
people_one: { | ||
name: 'Leia Skywalker', | ||
}, | ||
}; | ||
|
||
it('reruns query if the variables change', (done) => { | ||
const observable : ObservableQuery = mockWatchQuery({ | ||
request: { query, variables }, | ||
result: { data: dataOne }, | ||
}, { | ||
request: { query, variables: differentVariables }, | ||
result: { data: dataTwo }, | ||
}); | ||
|
||
let handleCount = 0; | ||
observable.subscribe({ | ||
next: wrap(done, result => { | ||
handleCount++; | ||
|
||
if (handleCount === 1) { | ||
assert.deepEqual(result.data, dataOne); | ||
observable.setVariables(differentVariables); | ||
} else if (handleCount === 2) { | ||
assert.deepEqual(result.data, dataTwo); | ||
done(); | ||
} | ||
}), | ||
}); | ||
}); | ||
|
||
|
||
it('does not rerun query if variables do not change', (done) => { | ||
const observable : ObservableQuery = mockWatchQuery({ | ||
request: { query, variables }, | ||
result: { data: dataOne }, | ||
}, { | ||
request: { query, variables }, | ||
result: { data: dataTwo }, | ||
}); | ||
|
||
let handleCount = 0; | ||
let errored = false; | ||
observable.subscribe({ | ||
next: wrap(done, result => { | ||
handleCount++; | ||
|
||
if (handleCount === 1) { | ||
assert.deepEqual(result.data, dataOne); | ||
observable.setVariables(variables); | ||
|
||
// Nothing should happen, so we'll wait a moment to check that | ||
setTimeout(() => !errored && done(), 10); | ||
} else if (handleCount === 2) { | ||
errored = true; | ||
throw new Error("Observable callback should not fire twice"); | ||
} | ||
}), | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
QueryManager, | ||
} from '../../src/QueryManager'; | ||
|
||
import mockNetworkInterface, { | ||
MockedResponse, | ||
} from './mockNetworkInterface'; | ||
|
||
import { | ||
createApolloStore, | ||
} from '../../src/store'; | ||
|
||
|
||
// Helper method for the tests that construct a query manager out of a | ||
// a list of mocked responses for a mocked network interface. | ||
export default (...mockedResponses: MockedResponse[]) => { | ||
return new QueryManager({ | ||
networkInterface: mockNetworkInterface(...mockedResponses), | ||
store: createApolloStore(), | ||
reduxRootKey: 'apollo', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import mockNetworkInterface, { | ||
MockedResponse, | ||
} from './mockNetworkInterface'; | ||
|
||
import mockQueryManager from './mockQueryManager'; | ||
|
||
import { ObservableQuery } from '../../src/ObservableQuery'; | ||
|
||
export default (...mockedResponses: MockedResponse[]) => { | ||
const queryManager = mockQueryManager(...mockedResponses); | ||
const firstRequest = mockedResponses[0].request; | ||
return queryManager.watchQuery({ | ||
query: firstRequest.query, | ||
variables: firstRequest.variables, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters