-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
114 lines (92 loc) · 2.87 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"use strict";
const assert = require("assert");
const fs = require("fs");
const { stripIndent } = require("common-tags");
const { Builder } = require("broccoli");
const GraphQLFilter = require(".");
const extractImports = require("./lib/extract-imports");
const FIXTURES_DIR = `${__dirname}/test/fixtures`;
const FIXTURES = fs.readdirSync(FIXTURES_DIR);
describe("Acceptance | File creation", function() {
for (let fixture of FIXTURES) {
describe(fixture, function() {
const cwd = `${FIXTURES_DIR}/${fixture}`;
const options = fs.existsSync(`${cwd}/options.json`)
? require(`${cwd}/options.json`)
: {};
const builder = new Builder(new GraphQLFilter(`${cwd}/input`, options))
before(function() {
return builder.build();
});
after(function() {
return builder.cleanup();
});
it("compiles files correctly", function() {
let actualFiles = fs.readdirSync(builder.outputPath).sort();
let expectedFiles = fs.readdirSync(`${cwd}/expected`).sort();
assert.deepEqual(actualFiles, expectedFiles);
for (let file of expectedFiles) {
assert.equal(
fs.readFileSync(`${builder.outputPath}/${file}`, "utf-8"),
fs.readFileSync(`${cwd}/expected/${file}`, "utf-8"),
`${file} was transpiled correctly`
);
}
});
});
}
});
describe("Unit | extractImports", function() {
it("extracts * imports", function() {
let source = stripIndent`
# import * from "./foo"
`;
assert.deepEqual(extractImports(source, 'test.gql'), [
{ source: `"./foo"` }
]);
});
it("extracts named imports", function() {
let source = stripIndent`
# import Foo, Bar from "./baz"
`;
assert.deepEqual(extractImports(source, 'test.gql'), [
{ source: `"./baz"`, bindings: ["Foo", "Bar"] }
]);
});
it("extracts legacy-format imports", function() {
let source = stripIndent`
#import "./foo"
`;
assert.deepEqual(extractImports(source, 'test.gql'), [
{ source: `"./foo"` }
]);
});
it("extracts all imports", function() {
let source = stripIndent`
# import * from "./foo"
# import Bar from "baz"
`;
assert.deepEqual(extractImports(source, 'test.gql'), [
{ source: `"./foo"` },
{ source: `"baz"`, bindings: ["Bar"] }
]);
});
it("rejects invalid import specifiers", function() {
let source = stripIndent`
# import Foo, * from "./foo"
`;
assert.throws(
() => extractImports(source, 'test.gql'),
new Error("A '*' import must be the only binding")
);
});
it("rejects invalid identifiers", function() {
let source = stripIndent`
# import foo-bar from "./foo"
`;
assert.throws(
() => extractImports(source, 'test.gql'),
new Error("Invalid import identifier: foo-bar")
);
});
});