Skip to content

Commit 163fbb4

Browse files
committed
Add tests for assertValidName
Tested with and without GRAPHQL_NO_NAME_WARNING in the environment.
1 parent 423200f commit 163fbb4

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"babel-plugin-transform-regenerator": "6.24.1",
6666
"chai": "3.5.0",
6767
"chai-json-equal": "0.0.1",
68+
"chai-spies": "0.7.1",
6869
"chai-subset": "1.5.0",
6970
"coveralls": "2.13.1",
7071
"eslint": "3.19.0",

resources/mocha-bootload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
*/
99

1010
var chai = require('chai');
11-
chai.use(require('chai-subset'));
1211
chai.use(require('chai-json-equal'));
12+
chai.use(require('chai-spies'));
13+
chai.use(require('chai-subset'));
1314

1415
process.on('unhandledRejection', function (error) {
1516
console.error('Unhandled Promise Rejection:');

src/utilities/__tests__/assertValidName-test.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10-
import { describe, it } from 'mocha';
11-
import { expect } from 'chai';
10+
import { afterEach, beforeEach, describe, it } from 'mocha';
11+
import { default as chai, expect } from 'chai';
1212
import { formatWarning } from '../assertValidName';
1313

14+
/* eslint-disable no-console */
15+
1416
/**
1517
* Helper for dedenting indented template literals. This helps us to
1618
* keep the tests pretty.
@@ -41,6 +43,64 @@ function createErrorObject(message, stack) {
4143
return error;
4244
}
4345

46+
describe('assertValidName()', () => {
47+
let assertValidName;
48+
let noNameWarning;
49+
let warn;
50+
51+
beforeEach(() => {
52+
noNameWarning = process.env.GRAPHQL_NO_NAME_WARNING;
53+
delete process.env.GRAPHQL_NO_NAME_WARNING;
54+
warn = console.warn;
55+
console.warn = chai.spy();
56+
57+
// Make sure module-internal state is reset for each test.
58+
delete require.cache[require.resolve('../assertValidName')];
59+
assertValidName = require('../assertValidName').assertValidName;
60+
61+
});
62+
63+
afterEach(() => {
64+
console.warn = warn;
65+
if (noNameWarning === undefined) {
66+
delete process.env.GRAPHQL_NO_NAME_WARNING;
67+
} else {
68+
process.env.GRAPHQL_NO_NAME_WARNING = noNameWarning;
69+
}
70+
});
71+
72+
it('warns against use of leading double underscores', () => {
73+
assertValidName('__bad');
74+
expect(console.warn).to.have.been.called.once();
75+
expect(console.warn.__spy.calls[0][0]).to.match(/must not begin with/);
76+
});
77+
78+
it('warns exactly once even in the presence of multiple violations', () => {
79+
assertValidName('__bad');
80+
assertValidName('__alsoBad');
81+
expect(console.warn).to.have.been.called.once();
82+
});
83+
84+
it('throws for non-strings', () => {
85+
expect(() => assertValidName({})).to.throw(/Must be named/);
86+
});
87+
88+
it('throws for names with invalid characters', () => {
89+
expect(() => assertValidName('>--()-->')).to.throw(/Names must match/);
90+
});
91+
92+
it('does not warn during introspection', () => {
93+
assertValidName('__bad', true);
94+
expect(console.warn).not.to.have.been.called();
95+
});
96+
97+
it('does not warn when GRAPHQL_NO_NAME_WARNING is in effect', () => {
98+
process.env.GRAPHQL_NO_NAME_WARNING = '1';
99+
assertValidName('__bad', true);
100+
expect(console.warn).not.to.have.been.called();
101+
});
102+
});
103+
44104
describe('formatWarning()', () => {
45105
it('formats given a Chrome-style stack property', () => {
46106
const chromeStack = dedent(`

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ [email protected]:
627627
version "0.0.1"
628628
resolved "https://registry.yarnpkg.com/chai-json-equal/-/chai-json-equal-0.0.1.tgz#338fcbbdaec63349379c7c4278c8a28da3b141a1"
629629

630+
chai-spies@^0.7.1:
631+
version "0.7.1"
632+
resolved "https://registry.yarnpkg.com/chai-spies/-/chai-spies-0.7.1.tgz#343d99f51244212e8b17e64b93996ff7b2c2a9b1"
633+
630634
631635
version "1.5.0"
632636
resolved "https://registry.yarnpkg.com/chai-subset/-/chai-subset-1.5.0.tgz#d03dbcfa8c9daad848643bbde4e63376b7882427"

0 commit comments

Comments
 (0)