Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@
"options": {
"mocha": "--require scripts/mocha-bootload src/**/__tests__/**/*.js"
},
"babel": {
"optional": ["runtime", "es7.asyncFunctions"]
},
"scripts": {
"prepublish": "npm test && npm run build",
"test": "npm run lint && npm run check && mocha $npm_package_options_mocha",
"testonly": "mocha $npm_package_options_mocha",
"lint": "eslint src",
"check": "flow check",
"cover": "babel-node node_modules/.bin/isparta cover --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",
"build": "rm -rf lib/* && babel src --ignore __tests__ --optional runtime --out-dir lib",
"watch": "babel --optional runtime scripts/watch.js | node",
"build": "rm -rf lib/* && babel src --ignore __tests__ --out-dir lib",
"watch": "babel scripts/watch.js | node",
"coveralls": "babel-node node_modules/.bin/isparta cover --report html node_modules/.bin/_mocha --report lcovonly -- $npm_package_options_mocha && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"dependencies": {
"babel-runtime": "5.6.12"
"babel-runtime": "5.7.0"
},
"devDependencies": {
"babel": "5.6.14",
"babel-core": "5.6.20",
"babel": "5.6.23",
"babel-core": "5.7.2",
"babel-eslint": "3.1.23",
"chai": "3.0.0",
"chai-as-promised": "5.1.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/mocha-bootload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

require('babel/register')({
optional: ['runtime']
optional: ['runtime', 'es7.asyncFunctions']
});

var chai = require('chai');
Expand Down
57 changes: 29 additions & 28 deletions src/__tests__/starWarsIntrospectionTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,9 @@ import { graphql } from '../graphql';
// 80+ char lines are useful in describe/it, so ignore in this file.
/*eslint-disable max-len */

/**
* Helper function to test a query and the expected response.
*/
function testQuery(query, expected) {
return expect(
graphql(StarWarsSchema, query)
).to.become({data: expected});
}


describe('Star Wars Introspection Tests', () => {
describe('Basic Introspection', () => {
it('Allows querying the schema for types', () => {
it('Allows querying the schema for types', async () => {
var query = `
query IntrospectionTypeQuery {
__schema {
Expand Down Expand Up @@ -85,10 +75,11 @@ describe('Star Wars Introspection Tests', () => {
]
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for query type', () => {
it('Allows querying the schema for query type', async () => {
var query = `
query IntrospectionQueryTypeQuery {
__schema {
Expand All @@ -105,10 +96,11 @@ describe('Star Wars Introspection Tests', () => {
},
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for a specific type', () => {
it('Allows querying the schema for a specific type', async () => {
var query = `
query IntrospectionDroidTypeQuery {
__type(name: "Droid") {
Expand All @@ -121,10 +113,11 @@ describe('Star Wars Introspection Tests', () => {
name: 'Droid'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for an object kind', () => {
it('Allows querying the schema for an object kind', async () => {
var query = `
query IntrospectionDroidKindQuery {
__type(name: "Droid") {
Expand All @@ -139,10 +132,11 @@ describe('Star Wars Introspection Tests', () => {
kind: 'OBJECT'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for an interface kind', () => {
it('Allows querying the schema for an interface kind', async () => {
var query = `
query IntrospectionCharacterKindQuery {
__type(name: "Character") {
Expand All @@ -157,10 +151,11 @@ describe('Star Wars Introspection Tests', () => {
kind: 'INTERFACE'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for object fields', () => {
it('Allows querying the schema for object fields', async () => {
var query = `
query IntrospectionDroidFieldsQuery {
__type(name: "Droid") {
Expand Down Expand Up @@ -217,10 +212,12 @@ describe('Star Wars Introspection Tests', () => {
]
}
};
return testQuery(query, expected);

var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for nested object fields', () => {
it('Allows querying the schema for nested object fields', async () => {
var query = `
query IntrospectionDroidNestedFieldsQuery {
__type(name: "Droid") {
Expand Down Expand Up @@ -295,10 +292,11 @@ describe('Star Wars Introspection Tests', () => {
]
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for field args', () => {
it('Allows querying the schema for field args', async () => {
var query = `
query IntrospectionQueryTypeQuery {
__schema {
Expand Down Expand Up @@ -372,10 +370,12 @@ describe('Star Wars Introspection Tests', () => {
}
};

return testQuery(query, expected);

var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows querying the schema for documentation', () => {
it('Allows querying the schema for documentation', async () => {
var query = `
query IntrospectionDroidDescriptionQuery {
__type(name: "Droid") {
Expand All @@ -390,7 +390,8 @@ describe('Star Wars Introspection Tests', () => {
description: 'A mechanical creature in the Star Wars universe.'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});
});
});
78 changes: 36 additions & 42 deletions src/__tests__/starWarsQueryTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,9 @@ import { graphql } from '../graphql';
// 80+ char lines are useful in describe/it, so ignore in this file.
/*eslint-disable max-len */

/**
* Helper function to test a query and the expected response.
*/
function testQuery(query, expected) {
return expect(
graphql(StarWarsSchema, query)
).to.become({data: expected});
}

/**
* Helper function to test a query with params and the expected response.
*/
function testQueryWithParams(query, params, expected) {
return expect(
graphql(StarWarsSchema, query, null, params)
).to.become({data: expected});
}

describe('Star Wars Query Tests', () => {
describe('Basic Queries', () => {
it('Correctly identifies R2-D2 as the hero of the Star Wars Saga', () => {
it('Correctly identifies R2-D2 as the hero of the Star Wars Saga', async () => {
var query = `
query HeroNameQuery {
hero {
Expand All @@ -48,10 +30,11 @@ describe('Star Wars Query Tests', () => {
name: 'R2-D2'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows us to query for the ID and friends of R2-D2', () => {
it('Allows us to query for the ID and friends of R2-D2', async () => {
var query = `
query HeroNameAndFriendsQuery {
hero {
Expand Down Expand Up @@ -80,12 +63,13 @@ describe('Star Wars Query Tests', () => {
]
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});
});

describe('Nested Queries', () => {
it('Allows us to query for the friends of friends of R2-D2', () => {
it('Allows us to query for the friends of friends of R2-D2', async () => {
var query = `
query NestedQuery {
hero {
Expand Down Expand Up @@ -158,12 +142,13 @@ describe('Star Wars Query Tests', () => {
]
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});
});

describe('Using IDs and query parameters to refetch objects', () => {
it('Allows us to query for Luke Skywalker directly, using his ID', () => {
it('Allows us to query for Luke Skywalker directly, using his ID', async () => {
var query = `
query FetchLukeQuery {
human(id: "1000") {
Expand All @@ -176,10 +161,11 @@ describe('Star Wars Query Tests', () => {
name: 'Luke Skywalker'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows us to create a generic query, then use it to fetch Luke Skywalker using his ID', () => {
it('Allows us to create a generic query, then use it to fetch Luke Skywalker using his ID', async () => {
var query = `
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
Expand All @@ -195,10 +181,11 @@ describe('Star Wars Query Tests', () => {
name: 'Luke Skywalker'
}
};
return testQueryWithParams(query, params, expected);
var result = await graphql(StarWarsSchema, query, null, params);
expect(result).to.deep.equal({ data: expected });
});

it('Allows us to create a generic query, then use it to fetch Han Solo using his ID', () => {
it('Allows us to create a generic query, then use it to fetch Han Solo using his ID', async () => {
var query = `
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
Expand All @@ -214,10 +201,11 @@ describe('Star Wars Query Tests', () => {
name: 'Han Solo'
}
};
return testQueryWithParams(query, params, expected);
var result = await graphql(StarWarsSchema, query, null, params);
expect(result).to.deep.equal({ data: expected });
});

it('Allows us to create a generic query, then pass an invalid ID to get null back', () => {
it('Allows us to create a generic query, then pass an invalid ID to get null back', async () => {
var query = `
query humanQuery($id: String!) {
human(id: $id) {
Expand All @@ -231,12 +219,13 @@ describe('Star Wars Query Tests', () => {
var expected = {
human: null
};
return testQueryWithParams(query, params, expected);
var result = await graphql(StarWarsSchema, query, null, params);
expect(result).to.deep.equal({ data: expected });
});
});

describe('Using aliases to change the key in the response', () => {
it('Allows us to query for Luke, changing his key with an alias', () => {
it('Allows us to query for Luke, changing his key with an alias', async () => {
var query = `
query FetchLukeAliased {
luke: human(id: "1000") {
Expand All @@ -249,10 +238,11 @@ describe('Star Wars Query Tests', () => {
name: 'Luke Skywalker'
},
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows us to query for both Luke and Leia, using two root fields and an alias', () => {
it('Allows us to query for both Luke and Leia, using two root fields and an alias', async () => {
var query = `
query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
Expand All @@ -271,12 +261,13 @@ describe('Star Wars Query Tests', () => {
name: 'Leia Organa'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});
});

describe('Uses fragments to express more complex queries', () => {
it('Allows us to query using duplicated content', () => {
it('Allows us to query using duplicated content', async() => {
var query = `
query DuplicateFields {
luke: human(id: "1000") {
Expand All @@ -299,10 +290,11 @@ describe('Star Wars Query Tests', () => {
homePlanet: 'Alderaan'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows us to use a fragment to avoid duplicating content', () => {
it('Allows us to use a fragment to avoid duplicating content', async () => {
var query = `
query UseFragment {
luke: human(id: "1000") {
Expand All @@ -328,12 +320,13 @@ describe('Star Wars Query Tests', () => {
homePlanet: 'Alderaan'
}
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});
});

describe('Using __typename to find the type of an object', () => {
it('Allows us to verify that R2-D2 is a droid', () => {
it('Allows us to verify that R2-D2 is a droid', async () => {
var query = `
query CheckTypeOfR2 {
hero {
Expand All @@ -348,7 +341,8 @@ describe('Star Wars Query Tests', () => {
name: 'R2-D2'
},
};
return testQuery(query, expected);
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});
});
});