-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-test.js
106 lines (88 loc) · 3.19 KB
/
index-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
/* eslint-disable no-console */
/* eslint-disable max-len */
import fs from 'fs';
import path from 'path';
import { parse, print } from 'graphql';
import generateSchema from './schema/index';
import chai, { expect } from 'chai';
import { enhanceSchemaWithQueryArguments } from '../schema//enhanceSchemaWithQueryArguments';
chai.use(require('chai-string')); // equalIgnoreSpaces
import {
SRC_DIR,
TEST_DIR,
TEST_GQL_DATA,
TEST_EXPECTED,
TEST_GENERATED,
TEST_GQL_EXTENSION,
ENCODING
} from '../constants';
let gqlFiles;
let expectedFiles;
const gqlFilesSources = {};
const expectedFilesSources = {};
const generatedFiles = [];
describe('add query arguments to schema', () => {
before(done => {
// read all *.graphql files from input directory
const gqlPath = path.join(SRC_DIR, TEST_DIR, TEST_GQL_DATA);
gqlFiles = fs
.readdirSync(gqlPath)
.filter(file => path.extname(file) === TEST_GQL_EXTENSION)
.sort();
gqlFiles.forEach(file => {
const filePath = path.join(SRC_DIR, TEST_DIR, TEST_GQL_DATA, file);
const source = fs.readFileSync(filePath, ENCODING);
gqlFilesSources[file] = source;
});
// read all *.graphql files in output-expected directory
const schemaPath = path.join(SRC_DIR, TEST_DIR, TEST_EXPECTED);
expectedFiles = fs
.readdirSync(schemaPath)
.filter(file => path.extname(file) === TEST_GQL_EXTENSION)
.sort();
expectedFiles.forEach(file => {
const filePath = path.join(SRC_DIR, TEST_DIR, TEST_EXPECTED, file);
const source = fs.readFileSync(filePath, ENCODING);
expectedFilesSources[file] = source;
});
done();
}); // before
it('should have equal number of files: "input" and "output-expected"', () => {
const gqlFilesCount = gqlFiles.length;
const expectedFilesCount = expectedFiles.length;
expect(gqlFilesCount).to.not.equal(0);
expect(gqlFilesCount).to.equal(expectedFilesCount);
});
it('generated schema files should be equal to expected files', done => {
gqlFiles.forEach(gqlFile => {
const schemaFileName = path.basename(gqlFile);
// generate schema code from .graphql file
const gqlSource = gqlFilesSources[gqlFile];
const inputSchema = parse(gqlSource);
const generatedSource = print(
enhanceSchemaWithQueryArguments(generateSchema(inputSchema))
);
const generateFilename = path.join(
SRC_DIR,
TEST_DIR,
TEST_GENERATED,
schemaFileName
);
fs.writeFileSync(generateFilename, generatedSource, ENCODING);
generatedFiles.push(generateFilename);
// the code can be parsed
const parsedGeneratedCode = parse(generatedSource);
expect(parsedGeneratedCode, generateFilename).to.not.equal({});
// get expected schema from output-expected file for comparison
const expectedSource = expectedFilesSources[schemaFileName];
// compare AST of both sources
expect(generatedSource, generateFilename).to.equal(expectedSource);
});
done();
});
after(done => {
console.log('\n Summary:\n ========');
generatedFiles.forEach(file => console.log(' Generated file: ', file));
done();
});
}); // describe getModelCode