@aws-amplify/amplify-util-uibuilder 1.5.0 → 1.6.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/amplify-plugin.json +10 -3
  3. package/lib/clients/amplify-studio-client.d.ts +13 -2
  4. package/lib/clients/amplify-studio-client.d.ts.map +1 -1
  5. package/lib/clients/amplify-studio-client.js +85 -17
  6. package/lib/clients/amplify-studio-client.js.map +1 -1
  7. package/lib/commands/generateComponents.d.ts.map +1 -1
  8. package/lib/commands/generateComponents.js +33 -15
  9. package/lib/commands/generateComponents.js.map +1 -1
  10. package/lib/commands/help.d.ts +1 -1
  11. package/lib/commands/help.d.ts.map +1 -1
  12. package/lib/commands/help.js +2 -2
  13. package/lib/commands/help.js.map +1 -1
  14. package/lib/commands/utils/codegenResources.d.ts +14 -3
  15. package/lib/commands/utils/codegenResources.d.ts.map +1 -1
  16. package/lib/commands/utils/codegenResources.js +72 -5
  17. package/lib/commands/utils/codegenResources.js.map +1 -1
  18. package/lib/commands/utils/featureFlags.d.ts +2 -0
  19. package/lib/commands/utils/featureFlags.d.ts.map +1 -0
  20. package/lib/commands/utils/featureFlags.js +35 -0
  21. package/lib/commands/utils/featureFlags.js.map +1 -0
  22. package/lib/commands/utils/getUiBuilderComponentsPath.d.ts.map +1 -1
  23. package/lib/commands/utils/getUiBuilderComponentsPath.js.map +1 -1
  24. package/lib/commands/utils/shouldRenderComponents.d.ts +1 -1
  25. package/lib/commands/utils/shouldRenderComponents.d.ts.map +1 -1
  26. package/lib/commands/utils/shouldRenderComponents.js +14 -8
  27. package/lib/commands/utils/shouldRenderComponents.js.map +1 -1
  28. package/lib/commands/utils/syncAmplifyUiBuilderComponents.d.ts +12 -20
  29. package/lib/commands/utils/syncAmplifyUiBuilderComponents.d.ts.map +1 -1
  30. package/lib/commands/utils/syncAmplifyUiBuilderComponents.js +51 -3
  31. package/lib/commands/utils/syncAmplifyUiBuilderComponents.js.map +1 -1
  32. package/lib/commands/version.d.ts +1 -1
  33. package/lib/commands/version.d.ts.map +1 -1
  34. package/lib/commands/version.js +2 -2
  35. package/lib/commands/version.js.map +1 -1
  36. package/lib/event-handlers/handle-PostEnvAdd.d.ts +1 -1
  37. package/lib/event-handlers/handle-PostEnvAdd.d.ts.map +1 -1
  38. package/lib/event-handlers/handle-PostEnvAdd.js +4 -6
  39. package/lib/event-handlers/handle-PostEnvAdd.js.map +1 -1
  40. package/lib/event-handlers/handle-PostPull.d.ts +1 -1
  41. package/lib/event-handlers/handle-PostPull.d.ts.map +1 -1
  42. package/lib/event-handlers/handle-PostPull.js +4 -3
  43. package/lib/event-handlers/handle-PostPull.js.map +1 -1
  44. package/lib/event-handlers/handle-PostPush.d.ts +3 -0
  45. package/lib/event-handlers/handle-PostPush.d.ts.map +1 -0
  46. package/lib/event-handlers/handle-PostPush.js +15 -0
  47. package/lib/event-handlers/handle-PostPush.js.map +1 -0
  48. package/package.json +8 -7
  49. package/src/__tests__/cloneComponentsFromEnv.test.ts +17 -6
  50. package/src/__tests__/createUiBuilderComponent.test.ts +26 -3
  51. package/src/__tests__/generateComponents.test.ts +44 -15
  52. package/src/__tests__/getAmplifyDataSchema.test.ts +51 -19
  53. package/src/__tests__/shouldRenderComponents.test.ts +51 -4
  54. package/src/__tests__/syncAmplifyUiComponents.test.ts +126 -8
  55. package/src/__tests__/utils.ts +70 -0
  56. package/src/clients/amplify-studio-client.ts +157 -69
  57. package/src/commands/generateComponents.ts +43 -16
  58. package/src/commands/help.ts +5 -2
  59. package/src/commands/utils/codegenResources.ts +115 -6
  60. package/src/commands/utils/featureFlags.ts +62 -0
  61. package/src/commands/utils/getUiBuilderComponentsPath.ts +7 -2
  62. package/src/commands/utils/shouldRenderComponents.ts +20 -8
  63. package/src/commands/utils/syncAmplifyUiBuilderComponents.ts +96 -29
  64. package/src/commands/version.ts +5 -2
  65. package/src/event-handlers/handle-PostEnvAdd.ts +8 -6
  66. package/src/event-handlers/handle-PostPull.ts +7 -3
  67. package/src/event-handlers/handle-PostPush.ts +15 -0
  68. package/tsconfig.tsbuildinfo +1 -1
  69. package/src/__tests__/handleEvents.test.ts +0 -21
@@ -7,8 +7,11 @@ jest.mock('amplify-cli-core');
7
7
  const awsMock = aws as any;
8
8
  const utilsMock = utils as any;
9
9
 
10
- utilsMock.shouldRenderComponents = jest.fn().mockImplementation(() => true);
11
- utilsMock.notifyMissingPackages = jest.fn().mockImplementation(() => true);
10
+ utilsMock.shouldRenderComponents = jest.fn().mockReturnValue(true);
11
+ utilsMock.notifyMissingPackages = jest.fn().mockReturnValue(true);
12
+ jest.mock('../commands/utils/featureFlags', () => ({
13
+ getTransformerVersion: jest.fn().mockImplementation(() => 2),
14
+ }));
12
15
 
13
16
  describe('can generate components', () => {
14
17
  let context: any;
@@ -36,27 +39,53 @@ describe('can generate components', () => {
36
39
  },
37
40
  ],
38
41
  };
39
- mockedExport = jest.fn(() => ({
42
+ mockedExport = jest.fn().mockReturnValue({
40
43
  entities: schemas.entities,
41
- }));
42
- awsMock.AmplifyUIBuilder = jest.fn(() => ({
43
- exportComponents: jest.fn(() => ({
44
+ });
45
+ awsMock.AmplifyUIBuilder = jest.fn().mockReturnValue({
46
+ exportComponents: jest.fn().mockReturnValue({
44
47
  promise: () => mockedExport(),
45
- })),
46
- exportThemes: jest.fn(() => ({
48
+ }),
49
+ exportThemes: jest.fn().mockReturnValue({
47
50
  promise: () => mockedExport(),
48
- })),
49
- }));
50
- utilsMock.generateUiBuilderComponents = jest.fn().mockImplementation(() => schemas.entities);
51
- utilsMock.generateUiBuilderThemes = jest.fn().mockImplementation(() => schemas.entities);
52
- utilsMock.getAmplifyDataSchema = jest.fn().mockImplementation(() => undefined);
53
- utilsMock.generateAmplifyUiBuilderIndexFile = jest.fn().mockImplementation(() => true);
51
+ }),
52
+ exportForms: jest.fn().mockReturnValue({
53
+ promise: () => mockedExport(),
54
+ }),
55
+ exportViews: jest.fn().mockReturnValue({
56
+ promise: () => mockedExport(),
57
+ }),
58
+ getMetadata: jest.fn().mockReturnValue({
59
+ promise: jest.fn().mockReturnValue({
60
+ features: {
61
+ autoGenerateForms: 'true',
62
+ autoGenerateViews: 'true',
63
+ },
64
+ }),
65
+ }),
66
+ });
67
+ utilsMock.generateUiBuilderComponents = jest.fn().mockReturnValue(schemas.entities);
68
+ utilsMock.generateUiBuilderThemes = jest.fn().mockReturnValue(schemas.entities);
69
+ utilsMock.generateUiBuilderForms = jest.fn().mockReturnValue(schemas.entities);
70
+ utilsMock.getAmplifyDataSchema = jest.fn().mockReturnValue(undefined);
71
+ utilsMock.generateAmplifyUiBuilderIndexFile = jest.fn().mockReturnValue(true);
72
+ utilsMock.generateAmplifyUiBuilderUtilFile = jest.fn().mockReturnValue(true);
54
73
  });
55
74
 
56
75
  it('runs generateComponents', async () => {
57
76
  await run(context);
58
- expect(mockedExport).toBeCalledTimes(2);
77
+ expect(mockedExport).toBeCalledTimes(3);
59
78
  expect(utilsMock.generateUiBuilderComponents).toBeCalledTimes(1);
60
79
  expect(utilsMock.generateUiBuilderThemes).toBeCalledTimes(1);
80
+ expect(utilsMock.generateUiBuilderForms).toBeCalledTimes(1);
81
+ });
82
+
83
+ it('does not run generateComponents if not Amplify Admin app', async () => {
84
+ utilsMock.shouldRenderComponents = jest.fn().mockReturnValue(false);
85
+ await run(context);
86
+ expect(mockedExport).toBeCalledTimes(0);
87
+ expect(utilsMock.generateUiBuilderComponents).toBeCalledTimes(0);
88
+ expect(utilsMock.generateUiBuilderThemes).toBeCalledTimes(0);
89
+ expect(utilsMock.generateUiBuilderForms).toBeCalledTimes(0);
61
90
  });
62
91
  });
@@ -1,6 +1,7 @@
1
1
  import aws from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies
2
- import { AmplifyCategories, AmplifySupportedService } from 'amplify-cli-core'; // eslint-disable-line import/no-extraneous-dependencies
2
+ import { AmplifyCategories, AmplifySupportedService, FeatureFlags } from 'amplify-cli-core'; // eslint-disable-line import/no-extraneous-dependencies
3
3
  import { printer } from 'amplify-prompts'; // eslint-disable-line import/no-extraneous-dependencies
4
+ import { mocked } from 'ts-jest/utils';
4
5
  import { AmplifyStudioClient } from '../clients';
5
6
  import { getAmplifyDataSchema } from '../commands/utils';
6
7
 
@@ -17,14 +18,19 @@ jest.mock('amplify-cli-core', () => ({
17
18
  },
18
19
  },
19
20
  })),
21
+ getCLIJSON: jest.fn().mockReturnValue({ features: { graphqltransformer: { transformerversion: 1 } } }),
22
+ setCLIJSON: jest.fn(),
20
23
  },
21
24
  FeatureFlags: {
22
- getBoolean: () => false,
23
- getNumber: () => 0,
25
+ getBoolean: () => true,
26
+ getNumber: jest.fn(),
27
+ reloadValues: jest.fn(),
24
28
  },
25
29
  }));
26
30
  jest.mock('amplify-prompts');
27
31
 
32
+ const mockedFeatureFlags = mocked(FeatureFlags);
33
+
28
34
  const getMockedSchema = jest.fn(
29
35
  // eslint-disable-next-line no-useless-escape, spellcheck/spell-checker
30
36
  () => `export const schema = {\n \"models\": {\n \"Blog\": {\n \"name\": \"Blog\",\n \"fields\": {\n \"id\": {\n \"name\": \"id\",\n \"isArray\": false,\n \"type\": \"ID\",\n \"isRequired\": true,\n \"attributes\": []\n },\n \"title\": {\n \"name\": \"title\",\n \"isArray\": false,\n \"type\": \"String\",\n \"isRequired\": false,\n \"attributes\": []\n },\n \"content\": {\n \"name\": \"content\",\n \"isArray\": false,\n \"type\": \"String\",\n \"isRequired\": false,\n \"attributes\": []\n },\n \"status\": {\n \"name\": \"status\",\n \"isArray\": false,\n \"type\": {\n \"enum\": \"Status\"\n },\n \"isRequired\": false,\n \"attributes\": []\n },\n \"createdAt\": {\n \"name\": \"createdAt\",\n \"isArray\": false,\n \"type\": \"AWSDateTime\",\n \"isRequired\": false,\n \"attributes\": [],\n \"isReadOnly\": true\n },\n \"updatedAt\": {\n \"name\": \"updatedAt\",\n \"isArray\": false,\n \"type\": \"AWSDateTime\",\n \"isRequired\": false,\n \"attributes\": [],\n \"isReadOnly\": true\n }\n },\n \"syncable\": true,\n \"pluralName\": \"Blogs\",\n \"attributes\": [\n {\n \"type\": \"model\",\n \"properties\": {}\n },\n {\n \"type\": \"auth\",\n \"properties\": {\n \"rules\": [\n {\n \"allow\": \"public\",\n \"operations\": [\n \"create\",\n \"update\",\n \"delete\",\n \"read\"\n ]\n }\n ]\n }\n }\n ]\n }\n },\n \"enums\": {\n \"Status\": {\n \"name\": \"Status\",\n \"values\": [\n \"ENABLED\",\n \"DISABLED\"\n ]\n }\n },\n \"nonModels\": {},\n \"version\": \"demo\"\n};`,
@@ -33,6 +39,20 @@ const getMockedSchema = jest.fn(
33
39
  describe('should sync amplify backend models', () => {
34
40
  let context: any;
35
41
 
42
+ beforeAll(() => {
43
+ // set metadata response
44
+ awsMock.AmplifyUIBuilder = jest.fn(() => ({
45
+ getMetadata: jest.fn(() => ({
46
+ promise: jest.fn(() => ({
47
+ features: {
48
+ autoGenerateForms: 'true',
49
+ autoGenerateViews: 'true',
50
+ },
51
+ })),
52
+ })),
53
+ }));
54
+ });
55
+
36
56
  beforeEach(() => {
37
57
  context = {
38
58
  exeInfo: {
@@ -56,17 +76,21 @@ describe('should sync amplify backend models', () => {
56
76
  envName: 'testEnvName',
57
77
  },
58
78
  },
79
+ print: { warning: jest.fn() },
59
80
  };
60
- });
61
81
 
62
- it('should getAmplifyBackendModels', async () => {
63
- awsMock.AmplifyBackend = jest.fn(() => ({
64
- getBackendAPIModels: jest.fn(() => ({
82
+ mockedFeatureFlags.getNumber.mockReturnValue(2);
83
+ getMockedSchema.mockClear();
84
+ awsMock.AmplifyBackend = jest.fn().mockReturnValue({
85
+ getBackendAPIModels: jest.fn().mockReturnValue({
65
86
  promise: () => ({
66
87
  Models: getMockedSchema(),
67
88
  }),
68
- })),
69
- }));
89
+ }),
90
+ });
91
+ });
92
+
93
+ it('should getAmplifyBackendModels', async () => {
70
94
  const client = await AmplifyStudioClient.setClientInfo(context);
71
95
  const dataSchema = await getAmplifyDataSchema(client);
72
96
  expect(getMockedSchema).toBeCalled();
@@ -74,14 +98,22 @@ describe('should sync amplify backend models', () => {
74
98
  expect(Object.keys(dataSchema!.models)).toContain('Blog');
75
99
  });
76
100
 
101
+ it('should not getAmplifyBackendModels and return undefined if graphQLTransformer version not supported', async () => {
102
+ mockedFeatureFlags.getNumber.mockReturnValue(1);
103
+ const client = await AmplifyStudioClient.setClientInfo(context);
104
+ const dataSchema = await getAmplifyDataSchema(client);
105
+ expect(getMockedSchema).not.toBeCalled();
106
+ expect(dataSchema).toBeUndefined();
107
+ });
108
+
77
109
  it('should handle Models not found', async () => {
78
- awsMock.AmplifyBackend = jest.fn(() => ({
79
- getBackendAPIModels: jest.fn(() => ({
80
- promise: jest.fn(() => ({
110
+ awsMock.AmplifyBackend = jest.fn().mockReturnValue({
111
+ getBackendAPIModels: jest.fn().mockReturnValue({
112
+ promise: () => ({
81
113
  Models: undefined,
82
- })),
83
- })),
84
- }));
114
+ }),
115
+ }),
116
+ });
85
117
  const client = await AmplifyStudioClient.setClientInfo(context);
86
118
  const dataSchema = await getAmplifyDataSchema(client);
87
119
  expect(dataSchema).toBeUndefined();
@@ -89,11 +121,11 @@ describe('should sync amplify backend models', () => {
89
121
  });
90
122
 
91
123
  it('should handle network error', async () => {
92
- awsMock.AmplifyBackend = jest.fn(() => ({
93
- getBackendAPIModels: jest.fn(() => ({
124
+ awsMock.AmplifyBackend = jest.fn().mockReturnValue({
125
+ getBackendAPIModels: jest.fn().mockReturnValue({
94
126
  promise: jest.fn().mockRejectedValue(new Error('Network Error')),
95
- })),
96
- }));
127
+ }),
128
+ });
97
129
  const client = await AmplifyStudioClient.setClientInfo(context);
98
130
  const dataSchema = await getAmplifyDataSchema(client);
99
131
  expect(dataSchema).toBeUndefined();
@@ -1,13 +1,60 @@
1
- import { $TSContext } from 'amplify-cli-core';
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+ import {
3
+ $TSContext, AmplifyCategories, AmplifySupportedService, CloudformationProviderFacade,
4
+ } from 'amplify-cli-core';
5
+ import aws from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies
2
6
  import { shouldRenderComponents } from '../commands/utils/shouldRenderComponents';
3
7
 
8
+ const awsMock = aws as any;
9
+
10
+ jest.mock('amplify-cli-core', () => ({
11
+ ...jest.requireActual('amplify-cli-core'),
12
+ stateManager: {
13
+ getMeta: jest.fn(() => ({
14
+ [AmplifyCategories.API]: {
15
+ MyResourceName: {
16
+ service: AmplifySupportedService.APPSYNC,
17
+ },
18
+ },
19
+ })),
20
+ // eslint-disable-next-line spellcheck/spell-checker
21
+ getCLIJSON: jest.fn().mockReturnValue({ features: { graphqltransformer: { transformerversion: 2 } } }),
22
+ setCLIJSON: jest.fn(),
23
+ },
24
+ FeatureFlags: {
25
+ getBoolean: () => true,
26
+ getNumber: jest.fn().mockReturnValue(2),
27
+ reloadValues: jest.fn(),
28
+ },
29
+ }));
30
+ CloudformationProviderFacade.isAmplifyAdminApp = jest.fn().mockReturnValue({
31
+ isAdminApp: true,
32
+ });
33
+
4
34
  describe('should render components', () => {
5
35
  let context: $TSContext | any;
6
- beforeEach(() => {
36
+
37
+ beforeAll(async () => {
38
+ // set metadata response
39
+ awsMock.AmplifyUIBuilder = jest.fn(() => ({
40
+ getMetadata: jest.fn(() => ({
41
+ promise: jest.fn(() => ({
42
+ features: {
43
+ autoGenerateForms: 'true',
44
+ autoGenerateViews: 'true',
45
+ },
46
+ })),
47
+ })),
48
+ }));
7
49
  context = {
50
+ amplify: {
51
+ invokePluginMethod: () => ({}),
52
+ },
8
53
  input: {
9
54
  options: {
10
55
  'no-codegen': false,
56
+ appId: 'testAppId',
57
+ envName: 'testEnvName',
11
58
  },
12
59
  },
13
60
  exeInfo: {
@@ -35,12 +82,12 @@ describe('should render components', () => {
35
82
  const shouldIt = await shouldRenderComponents(context);
36
83
  expect(shouldIt).toBe(false);
37
84
  });
38
- it("doesn't work if the frontend is not javascript", async () => {
85
+ it('should return false if frontend is ios', async () => {
39
86
  context.exeInfo.projectConfig.frontend = 'ios';
40
87
  const shouldIt = await shouldRenderComponents(context);
41
88
  expect(shouldIt).toBe(false);
42
89
  });
43
- it("doesn't work if the frontend is not javascript", async () => {
90
+ it('should return false if frontend is vue', async () => {
44
91
  context.exeInfo.projectConfig.javascript.framework = 'vue';
45
92
  const shouldIt = await shouldRenderComponents(context);
46
93
  expect(shouldIt).toBe(false);
@@ -1,3 +1,4 @@
1
+ /* eslint-disable spellcheck/spell-checker */
1
2
  import { AmplifyCategories, AmplifySupportedService, stateManager } from 'amplify-cli-core'; // eslint-disable-line import/no-extraneous-dependencies
2
3
  import aws from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies
3
4
  import {
@@ -6,9 +7,11 @@ import {
6
7
  getEnvName,
7
8
  getAppId,
8
9
  resolveAppId,
10
+ generateUiBuilderForms,
9
11
  } from '../commands/utils';
10
12
  import { AmplifyStudioClient } from '../clients';
11
13
  import * as createUiBuilderComponentDependency from '../commands/utils/codegenResources';
14
+ import { exampleSchema } from './utils';
12
15
 
13
16
  jest.mock('amplify-cli-core', () => ({
14
17
  ...jest.requireActual('amplify-cli-core'),
@@ -17,6 +20,9 @@ jest.mock('amplify-cli-core', () => ({
17
20
  getNumber: () => 0,
18
21
  },
19
22
  }));
23
+ jest.mock('../commands/utils/featureFlags', () => ({
24
+ getTransformerVersion: jest.fn().mockReturnValue(2),
25
+ }));
20
26
 
21
27
  const awsMock = aws as any;
22
28
  const stateManagerMock = stateManager as any;
@@ -64,12 +70,12 @@ describe('should sync amplify ui builder components', () => {
64
70
  promise: jest.fn(() => ({
65
71
  entities: [
66
72
  {
67
- appId: 'd37nrm8rzt3oek',
73
+ appId: 'd37nrm8rzt3oek', // eslint-disable-line spellcheck/spell-checker
68
74
  bindingProperties: {},
69
75
  componentType: 'Box',
70
76
  environmentName: 'staging',
71
- id: 's-s4mU579Ycf6JGHwhqT',
72
- name: 'aawwdd',
77
+ id: 's-s4mU579Ycf6JGHwhqT', // eslint-disable-line spellcheck/spell-checker
78
+ name: 'aawwdd', // eslint-disable-line spellcheck/spell-checker
73
79
  overrides: {},
74
80
  properties: {},
75
81
  variants: [],
@@ -82,10 +88,62 @@ describe('should sync amplify ui builder components', () => {
82
88
  entities: [{}],
83
89
  })),
84
90
  })),
91
+ exportForms: jest.fn(() => ({
92
+ promise: jest.fn(() => ({
93
+ entities: [{
94
+ name: 'BasicFormCreate',
95
+ formActionType: 'create',
96
+ dataType: {
97
+ dataSourceType: 'Custom',
98
+ dataTypeName: 'Post',
99
+ },
100
+ fields: {
101
+ name: {
102
+ inputType: {
103
+ required: true,
104
+ type: 'TextField',
105
+ name: 'name',
106
+ defaultValue: 'John Doe',
107
+ },
108
+ label: 'name',
109
+ },
110
+ },
111
+ sectionalElements: {},
112
+ style: {},
113
+ }],
114
+ })),
115
+ })),
116
+ exportViews: jest.fn(() => ({
117
+ promise: jest.fn(() => ({
118
+ entities: [{
119
+ appId: '23342',
120
+ dataSource: { type: 'Custom' },
121
+ environmentName: 'staging',
122
+ id: 'id',
123
+ name: 'ProductTable',
124
+ // TODO: replace with export when Codegen updated
125
+ schemaVersion: '1.0',
126
+ style: {},
127
+ viewConfiguration: {
128
+ type: 'Table',
129
+ },
130
+ }],
131
+ })),
132
+ })),
133
+ getMetadata: jest.fn(() => ({
134
+ promise: jest.fn(() => ({
135
+ features: {
136
+ autoGenerateForms: 'true',
137
+ autoGenerateViews: 'true',
138
+ },
139
+ })),
140
+ })),
85
141
  }));
86
142
 
87
143
  createUiBuilderComponentDependencyMock.createUiBuilderComponent = jest.fn();
88
144
  createUiBuilderComponentDependencyMock.createUiBuilderTheme = jest.fn();
145
+ createUiBuilderComponentDependencyMock.createUiBuilderForm = jest.fn();
146
+ createUiBuilderComponentDependencyMock.createUiBuilderView = jest.fn();
89
147
  });
90
148
 
91
149
  it('pulls components from aws-sdk and passes them to createUiBuilderComponent', () => {
@@ -94,14 +152,14 @@ describe('should sync amplify ui builder components', () => {
94
152
 
95
153
  it('does not throw an error when createUiBuilderComponent fails', () => {
96
154
  createUiBuilderComponentDependencyMock.createUiBuilderComponent = jest.fn(() => {
97
- throw new Error('ahhh!');
155
+ throw new Error('ahhh!'); // eslint-disable-line spellcheck/spell-checker
98
156
  });
99
157
  expect(async () => generateUiBuilderComponents(context, [])).not.toThrow();
100
158
  });
101
159
 
102
160
  it('does not throw an error when createUiBuilderThemes fails', () => {
103
161
  createUiBuilderComponentDependencyMock.createUiBuilderComponent = jest.fn(() => {
104
- throw new Error('ahhh!');
162
+ throw new Error('ahhh!'); // eslint-disable-line spellcheck/spell-checker
105
163
  });
106
164
  expect(async () => generateUiBuilderThemes(context, [])).not.toThrow();
107
165
  });
@@ -113,19 +171,30 @@ describe('should sync amplify ui builder components', () => {
113
171
  expect(Object.keys(client)).toEqual(expect.arrayContaining([
114
172
  'listComponents',
115
173
  'listThemes',
174
+ 'listForms',
116
175
  'getModels',
176
+ 'loadMetadata',
117
177
  'createComponent',
118
178
  ]));
179
+ expect(client.metadata).toEqual(expect.objectContaining({
180
+ autoGenerateForms: true,
181
+ autoGenerateViews: true,
182
+ }));
183
+ });
184
+ it('can list components', async () => {
185
+ const client = await AmplifyStudioClient.setClientInfo(context);
186
+ const components = await client.listComponents();
187
+ expect(components.entities).toHaveLength(1);
119
188
  });
120
189
  it('can list themes', async () => {
121
190
  const client = await AmplifyStudioClient.setClientInfo(context);
122
191
  const themes = await client.listThemes();
123
192
  expect(themes.entities).toHaveLength(1);
124
193
  });
125
- it('can listUiBuilderComponents', async () => {
194
+ it('can list forms', async () => {
126
195
  const client = await AmplifyStudioClient.setClientInfo(context);
127
- const components = await client.listThemes();
128
- expect(components.entities).toHaveLength(1);
196
+ const forms = await client.listForms();
197
+ expect(forms.entities).toHaveLength(1);
129
198
  });
130
199
  it('can getAppId', async () => {
131
200
  const appId = getAppId(context);
@@ -169,4 +238,53 @@ describe('should sync amplify ui builder components', () => {
169
238
  const themes = generateUiBuilderThemes(context, [{}, {}]);
170
239
  expect(themes.every(theme => theme.resultType === 'FAILURE')).toBeTruthy();
171
240
  });
241
+ it('can generate ui builder default theme when no themes are passed', async () => {
242
+ createUiBuilderComponentDependencyMock.createUiBuilderTheme = jest.fn().mockImplementation(() => ({}));
243
+ const themes = generateUiBuilderThemes(context, []);
244
+ expect(themes.every(theme => theme.resultType === 'SUCCESS')).toBeTruthy();
245
+ });
246
+ it('can handle failed generation generate ui builder default theme when no themes are passed', async () => {
247
+ createUiBuilderComponentDependencyMock.createUiBuilderTheme = jest.fn().mockImplementation(() => {
248
+ throw new Error('ahh!');
249
+ });
250
+ const themes = generateUiBuilderThemes(context, []);
251
+ expect(themes.every(theme => theme.resultType === 'FAILURE')).toBeTruthy();
252
+ });
253
+ it('can generate ui builder forms', async () => {
254
+ createUiBuilderComponentDependencyMock.createUiBuilderForm = jest.fn().mockImplementation(() => ({}));
255
+ const forms = generateUiBuilderForms(context, [{}, {}]);
256
+ expect(forms.every(form => form.resultType === 'SUCCESS')).toBeTruthy();
257
+ });
258
+ it('can handle failed generation generate ui builder forms', async () => {
259
+ createUiBuilderComponentDependencyMock.createUiBuilderForm = jest.fn().mockImplementation(() => {
260
+ throw new Error('ahh!');
261
+ });
262
+ const forms = generateUiBuilderForms(context, [{}, {}]);
263
+ expect(forms.every(form => form.resultType === 'FAILURE')).toBeTruthy();
264
+ });
265
+ it('can generate uibuilder forms from data schema if autogen true', async () => {
266
+ createUiBuilderComponentDependencyMock.createUiBuilderForm = jest.fn().mockImplementation(() => ({}));
267
+ const forms = generateUiBuilderForms(context, [], exampleSchema, true);
268
+ expect(forms.every(form => form.resultType === 'SUCCESS')).toBeTruthy();
269
+ // create & update form for author model
270
+ expect(forms.length).toEqual(2);
271
+ });
272
+
273
+ it('should not autogen forms for join tables or unsupported models', async () => {
274
+ expect(Object.keys(exampleSchema.models)).toStrictEqual(['Author', 'JoinTable', 'EmptyModel']);
275
+ createUiBuilderComponentDependencyMock.createUiBuilderForm = jest.fn().mockImplementation(
276
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
277
+ (_ctx, schema, _dataSchema) => ({ name: schema.dataType.dataTypeName }),
278
+ );
279
+ const forms = generateUiBuilderForms(context, [], exampleSchema, true);
280
+ expect(forms.every(form => form.resultType === 'SUCCESS')).toBeTruthy();
281
+ // only create & update form for author model
282
+ expect(forms.map(f => (f.schema as any).name)).toStrictEqual(['Author', 'Author']);
283
+ });
284
+
285
+ it('does not generate uibuilder forms from data schema if autogen false', async () => {
286
+ createUiBuilderComponentDependencyMock.createUiBuilderForm = jest.fn().mockImplementation(() => ({}));
287
+ const forms = generateUiBuilderForms(context, [], exampleSchema, false);
288
+ expect(forms.length).toEqual(0);
289
+ });
172
290
  });
@@ -0,0 +1,70 @@
1
+ import type { GenericDataSchema } from '@aws-amplify/codegen-ui';
2
+
3
+ export const exampleSchema: GenericDataSchema = {
4
+ dataSourceType: 'DataStore',
5
+ models: {
6
+ Author: {
7
+ fields: {
8
+ id: {
9
+ dataType: 'ID',
10
+ required: true,
11
+ readOnly: false,
12
+ isArray: false,
13
+ },
14
+ name: {
15
+ dataType: 'String',
16
+ required: false,
17
+ readOnly: false,
18
+ isArray: false,
19
+ },
20
+ profileImageSrc: {
21
+ dataType: 'AWSURL',
22
+ required: false,
23
+ readOnly: false,
24
+ isArray: false,
25
+ },
26
+ description: {
27
+ dataType: 'String',
28
+ required: false,
29
+ readOnly: false,
30
+ isArray: false,
31
+ },
32
+ books: {
33
+ dataType: 'String',
34
+ required: false,
35
+ readOnly: false,
36
+ isArray: true,
37
+ },
38
+ createdAt: {
39
+ dataType: 'AWSDateTime',
40
+ required: false,
41
+ readOnly: true,
42
+ isArray: false,
43
+ },
44
+ updatedAt: {
45
+ dataType: 'AWSDateTime',
46
+ required: false,
47
+ readOnly: true,
48
+ isArray: false,
49
+ },
50
+ },
51
+ },
52
+ JoinTable: {
53
+ fields: {
54
+ id: {
55
+ dataType: 'ID',
56
+ required: true,
57
+ readOnly: false,
58
+ isArray: false,
59
+ },
60
+ },
61
+ isJoinTable: true
62
+ },
63
+ EmptyModel: {
64
+ fields: {
65
+ },
66
+ },
67
+ },
68
+ enums: {}, // eslint-disable-line spellcheck/spell-checker
69
+ nonModels: {},
70
+ };