Skip to content

Commit a2b3d52

Browse files
author
Dane Pilcher
authored
test: add tests for overrideOutputDir and fix assertions (#675)
1 parent 10570ce commit a2b3d52

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`command-models-generates models in expected output path android: Should generate models from a single schema file 1`] = `
4+
Array [
5+
"AmplifyModelProvider.java",
6+
"SimpleModel.java",
7+
]
8+
`;
9+
10+
exports[`command-models-generates models in expected output path android: Should generate models from any subdirectory in schema folder 1`] = `
11+
Array [
12+
"AmplifyModelProvider.java",
13+
"SimpleModel.java",
14+
]
15+
`;
16+
17+
exports[`command-models-generates models in expected output path flutter: Should generate models from a single schema file 1`] = `
18+
Array [
19+
"ModelProvider.dart",
20+
"SimpleModel.dart",
21+
]
22+
`;
23+
24+
exports[`command-models-generates models in expected output path flutter: Should generate models from any subdirectory in schema folder 1`] = `
25+
Array [
26+
"ModelProvider.dart",
27+
"SimpleModel.dart",
28+
]
29+
`;
30+
31+
exports[`command-models-generates models in expected output path ios: Should generate models from a single schema file 1`] = `
32+
Array [
33+
"AmplifyModels.swift",
34+
"SimpleModel+Schema.swift",
35+
"SimpleModel.swift",
36+
]
37+
`;
38+
39+
exports[`command-models-generates models in expected output path ios: Should generate models from any subdirectory in schema folder 1`] = `
40+
Array [
41+
"AmplifyModels.swift",
42+
"SimpleModel+Schema.swift",
43+
"SimpleModel.swift",
44+
]
45+
`;
46+
47+
exports[`command-models-generates models in expected output path javascript: Should generate models from a single schema file 1`] = `
48+
Array [
49+
"index.d.ts",
50+
"index.js",
51+
"schema.d.ts",
52+
"schema.js",
53+
]
54+
`;
55+
56+
exports[`command-models-generates models in expected output path javascript: Should generate models from any subdirectory in schema folder 1`] = `
57+
Array [
58+
"index.d.ts",
59+
"index.js",
60+
"schema.d.ts",
61+
"schema.js",
62+
]
63+
`;

packages/amplify-codegen/tests/commands/models.test.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const MOCK_CONTEXT = {
3535
},
3636
};
3737
const OUTPUT_PATHS = {
38-
javascript: 'src',
39-
android: 'app/src/main/java',
38+
javascript: 'src/models',
39+
android: 'app/src/main/java/com/amplifyframework/datastore/generated/model',
4040
ios: 'amplify/generated/models',
4141
flutter: 'lib/models',
4242
};
@@ -60,7 +60,7 @@ describe('command-models-generates models in expected output path', () => {
6060
const outputDirectory = path.join(MOCK_PROJECT_ROOT, OUTPUT_PATHS[frontend]);
6161
const mockedFiles = {};
6262
mockedFiles[schemaFilePath] = {
63-
'schema.graphql': ' type SimpleModel { id: ID! status: String } ',
63+
'schema.graphql': ' type SimpleModel @model { id: ID! status: String } ',
6464
};
6565
mockedFiles[outputDirectory] = {};
6666
mockFs(mockedFiles);
@@ -75,7 +75,7 @@ describe('command-models-generates models in expected output path', () => {
7575
expect(graphqlCodegen.codegen).toBeCalled();
7676

7777
// assert model files are generated in expected output directory
78-
expect(fs.readdirSync(outputDirectory).length).toBeGreaterThan(0);
78+
expect(fs.readdirSync(outputDirectory)).toMatchSnapshot();
7979
});
8080

8181
it(frontend + ': Should generate models from any subdirectory in schema folder', async () => {
@@ -99,7 +99,35 @@ describe('command-models-generates models in expected output path', () => {
9999
expect(graphqlCodegen.codegen).toBeCalled();
100100

101101
// assert model files are generated in expected output directory
102-
expect(fs.readdirSync(outputDirectory).length).toBeGreaterThan(0);
102+
expect(fs.readdirSync(outputDirectory)).toMatchSnapshot();
103+
});
104+
105+
it(frontend + ': Should generate models in overrideOutputDir', async () => {
106+
// mock the input and output file structure
107+
const schemaFilePath = path.join(MOCK_BACKEND_DIRECTORY, 'api', MOCK_PROJECT_NAME);
108+
const outputDirectory = path.join(MOCK_PROJECT_ROOT, OUTPUT_PATHS[frontend]);
109+
const mockedFiles = {};
110+
mockedFiles[schemaFilePath] = {
111+
'schema.graphql': ' type SimpleModel @model { id: ID! status: String } ',
112+
};
113+
const overrideOutputDir = 'some/other/dir';
114+
mockedFiles[outputDirectory] = {};
115+
mockedFiles[overrideOutputDir] = {};
116+
mockFs(mockedFiles);
117+
MOCK_CONTEXT.amplify.getProjectConfig.mockReturnValue({ frontend: frontend });
118+
119+
// assert empty folder before generation
120+
expect(fs.readdirSync(outputDirectory).length).toEqual(0);
121+
expect(fs.readdirSync(overrideOutputDir).length).toEqual(0);
122+
123+
await generateModels(MOCK_CONTEXT, { overrideOutputDir });
124+
125+
// assert model generation succeeds with a single schema file
126+
expect(graphqlCodegen.codegen).toBeCalled();
127+
128+
// assert model files are generated in expected output directory
129+
expect(fs.readdirSync(outputDirectory).length).toEqual(0);
130+
expect(fs.readdirSync(overrideOutputDir).length).not.toEqual(0);
103131
});
104132

105133
if (frontend === 'flutter') {

0 commit comments

Comments
 (0)