-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add tests #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jlengstorf
wants to merge
2
commits into
master
Choose a base branch
from
feat/add-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -84,5 +84,5 @@ | |
| } | ||
| } | ||
| }, | ||
| "version": "0.0.0-development" | ||
| "version": "1.0.0-beta1" | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| export default { | ||
| Query: { | ||
| // TODO: Update query resolver name and args to match the schema | ||
| // TODO: Update the context method to load the correct data | ||
| trivia: (_, { number }, context) => context.get(number, 'trivia'), | ||
| date: (_, { date }, context) => context.get(date, 'date'), | ||
| math: (_, { number }, context) => context.get(number, 'math'), | ||
| year: (_, { number }, context) => context.get(number, 'year'), | ||
| } | ||
| trivia: (_, { number }, context) => context.model.get(number, 'trivia'), | ||
| date: (_, { date }, context) => context.model.get(date, 'date'), | ||
| math: (_, { number }, context) => context.model.get(number, 'math'), | ||
| year: (_, { number }, context) => context.model.get(number, 'year'), | ||
| }, | ||
| Numbers_Trivia: { | ||
| date: data => data.date || null, | ||
| year: data => data.year || null, | ||
| }, | ||
| }; | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,34 @@ | ||
| type Query { | ||
| trivia(number: Int): Numbers_Trivia | ||
| date(date: String): Numbers_Trivia | ||
| math(number: Int): Numbers_Trivia | ||
| year(number: Int): Numbers_Trivia | ||
| trivia(number: Int!): Numbers_Trivia! | ||
|
|
||
| """ | ||
| Load trivia for a data using the `MM/DD` format. | ||
|
|
||
| For example, to load trivia for January 14th: | ||
|
|
||
| `date(date: "1/14") { text }` | ||
| """ | ||
| date(date: String!): Numbers_Trivia! | ||
| math(number: Int!): Numbers_Trivia! | ||
| year(number: Int!): Numbers_Trivia! | ||
| } | ||
|
|
||
| type Numbers_Trivia { | ||
| text: String | ||
| found: Boolean | ||
| number: Int | ||
| type: String | ||
| "A string of the fact text itself." | ||
| text: String! | ||
|
|
||
| "Boolean of whether there was a fact for the requested number." | ||
| found: Boolean! | ||
|
|
||
| "The number that was queried." | ||
| number: Int! | ||
|
|
||
| "String of the category of the returned fact." | ||
| type: String! | ||
|
|
||
| "If relevant, a day of year associated with some year facts, as a string." | ||
| date: String | ||
|
|
||
| "If relevant, a year associated with some date facts, as a string." | ||
| year: String | ||
| } |
This file contains hidden or 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,14 @@ | ||
| import { GraphQLConnector } from '@gramps/rest-helpers'; | ||
| import Connector from '../src/connector'; | ||
|
|
||
| const connector = new Connector(); | ||
|
|
||
| describe(`NumbersConnector`, () => { | ||
| it('inherits the GraphQLConnector class', () => { | ||
| expect(connector).toBeInstanceOf(GraphQLConnector); | ||
| }); | ||
|
|
||
| it('uses the appropriate URL', () => { | ||
| expect(connector.apiBaseUri).toBe(`http://numbersapi.com`); | ||
| }); | ||
| }); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| import expectMockFields from './helpers/expectMockFields'; | ||
| import expectMockList from './helpers/expectMockList'; | ||
| import mocks from '../src/mocks'; | ||
|
|
||
| describe('mock resolvers', () => { | ||
| describe('PFX_DataSourceBase', () => { | ||
| const mockResolvers = mocks.PFX_DataSourceBase(); | ||
| describe('Numbers_Trivia', () => { | ||
| const mockResolvers = mocks.Numbers_Trivia(); | ||
|
|
||
| // This helper creates a test to ensure each field has a mock resolver. | ||
| expectMockFields(mockResolvers, ['id', 'name', 'lucky_numbers']); | ||
|
|
||
| // This helper creates a test to check that these fields return `MockList`s. | ||
| expectMockList(mockResolvers, ['lucky_numbers']); | ||
| expectMockFields(mockResolvers, [ | ||
| 'text', | ||
| 'found', | ||
| 'number', | ||
| 'type', | ||
| 'date', | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or 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,95 @@ | ||
| import { GraphQLModel } from '@gramps/rest-helpers'; | ||
| import Model from '../src/model'; | ||
| import Connector from '../src/connector'; | ||
|
|
||
| // Mock the connector because we’re only testing the model here. | ||
| jest.mock('../src/connector', () => | ||
| jest.fn(() => ({ | ||
| get: jest.fn(() => Promise.resolve()), | ||
| apiBaseUri: 'https://example.org', | ||
| })), | ||
| ); | ||
|
|
||
| const DATA_SOURCE_NAME = 'Numbers'; | ||
|
|
||
| const connector = new Connector(); | ||
| const model = new Model({ connector }); | ||
|
|
||
| describe(`${DATA_SOURCE_NAME}Model`, () => { | ||
| it('inherits the GraphQLModel class', () => { | ||
| expect(model).toBeInstanceOf(GraphQLModel); | ||
| }); | ||
|
|
||
| describe('get()', () => { | ||
| it('calls the correct endpoint to load the number trivia', () => { | ||
| const spy = jest.spyOn(connector, 'get'); | ||
|
|
||
| model.get(1234, 'trivia'); | ||
| expect(spy).toHaveBeenCalledWith('/1234/trivia'); | ||
| }); | ||
|
|
||
| it('throws a GrampsError if something goes wrong', async () => { | ||
| expect.assertions(1); | ||
|
|
||
| model.connector.get.mockImplementationOnce(() => | ||
| Promise.reject(Error('boom')), | ||
| ); | ||
|
|
||
| try { | ||
| await model.get(1, 'trivia'); | ||
| } catch (error) { | ||
| expect(error.isBoom).toEqual(true); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe.skip('throwError()', () => { | ||
| const mockError = { | ||
| statusCode: 401, | ||
| }; | ||
|
|
||
| it('converts an error from the endpoint into a GrampsError', async () => { | ||
| expect.assertions(4); | ||
|
|
||
| /* | ||
| * To simulate a failed call, we tell Jest to return a rejected Promise | ||
| * with our mock error. | ||
| */ | ||
| model.connector.get.mockImplementationOnce(() => | ||
| Promise.reject(mockError), | ||
| ); | ||
|
|
||
| try { | ||
| await model.get(1234, 'trivia'); | ||
| } catch (error) { | ||
| // Check that GrampsError properly received the error detail. | ||
| expect(error).toHaveProperty('isBoom', true); | ||
| expect(error.output).toHaveProperty('statusCode', 401); | ||
| expect(error.output.payload).toHaveProperty( | ||
| 'targetEndpoint', | ||
| 'https://example.org/1234/info.0.json', | ||
| ); | ||
| expect(error.output.payload).toHaveProperty( | ||
| 'graphqlModel', | ||
| `${DATA_SOURCE_NAME}Model`, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it('creates a default GrampsError if no custom error data is supplied', async () => { | ||
| try { | ||
| await model.throwError({}); | ||
| } catch (error) { | ||
| expect(error.output.statusCode).toBe(500); | ||
| expect(error.output.payload.errorCode).toBe( | ||
| `${DATA_SOURCE_NAME}Model_Error`, | ||
| ); | ||
| expect(error.output.payload.description).toBe('Something went wrong.'); | ||
| expect(error.output.payload.graphqlModel).toBe( | ||
| `${DATA_SOURCE_NAME}Model`, | ||
| ); | ||
| expect(error.output.payload.targetEndpoint).toBeNull(); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you add resolvers for the optional response fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
dateandyearfields aren’t always returned, so they might come backundefined. This way we can make sure nothing explodes and we’re explicitly returningnullif these aren’t set.