Skip to content

Commit

Permalink
add amplifyJsLibraryVersion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronZyLee committed Mar 28, 2024
1 parent 038cd00 commit 7b11ab7
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 10 deletions.
25 changes: 15 additions & 10 deletions packages/amplify-codegen/tests/commands/types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ const MOCK_APIS = [

getFrontEndHandler.mockReturnValue('javascript');

const MOCK_ANGULAR_PROJECT_BASE = {
excludes: [MOCK_EXCLUDE_PATH],
includes: [MOCK_INCLUDE_PATH],
schema: MOCK_SCHEMA,
amplifyExtension: {
generatedFileName: MOCK_GENERATED_FILE_NAME,
codeGenTarget: 'angular',
graphQLApiId: MOCK_API_ID,
region: MOCK_REGION,
amplifyJsLibraryVersion: 5,
},
};

describe('command - types', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -167,17 +180,9 @@ describe('command - types', () => {

it('should show a warning if the amplifyJsLibraryVersion is invalid', async () => {
const MOCK_ANGULAR_PROJECT = {
excludes: [MOCK_EXCLUDE_PATH],
includes: [MOCK_INCLUDE_PATH],
schema: MOCK_SCHEMA,
amplifyExtension: {
generatedFileName: MOCK_GENERATED_FILE_NAME,
codeGenTarget: 'angular',
graphQLApiId: MOCK_API_ID,
region: MOCK_REGION,
amplifyJsLibraryVersion: 7,
},
...MOCK_ANGULAR_PROJECT_BASE
};
MOCK_ANGULAR_PROJECT.amplifyExtension.amplifyJsLibraryVersion = 7
fs.readFileSync
.mockReturnValueOnce('query 1')
.mockReturnValueOnce('query 2')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,202 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generateTypes amplifyJsLibraryVersion generates angular types for v5 when value is 5 or undefined 1`] = `
Object {
"api.service.ts": "/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.
import { Injectable } from \\"@angular/core\\";
import API, { graphqlOperation, GraphQLResult } from \\"@aws-amplify/api-graphql\\";
import { Observable } from \\"zen-observable-ts\\";
export interface SubscriptionResponse<T> {
value: GraphQLResult<T>;
}
export type Blog = {
__typename: \\"Blog\\";
id: string;
name: string;
posts?: ModelPostConnection | null;
createdAt: string;
updatedAt: string;
};
export type ModelPostConnection = {
__typename: \\"ModelPostConnection\\";
items: Array<Post | null>;
nextToken?: string | null;
};
export type Post = {
__typename: \\"Post\\";
id: string;
title: string;
blog?: Blog | null;
comments?: ModelCommentConnection | null;
createdAt: string;
updatedAt: string;
blogPostsId?: string | null;
};
export type ModelCommentConnection = {
__typename: \\"ModelCommentConnection\\";
items: Array<Comment | null>;
nextToken?: string | null;
};
export type Comment = {
__typename: \\"Comment\\";
id: string;
post?: Post | null;
content: string;
createdAt: string;
updatedAt: string;
postCommentsId?: string | null;
};
export type GetBlogQuery = {
__typename: \\"Blog\\";
id: string;
name: string;
posts?: {
__typename: \\"ModelPostConnection\\";
nextToken?: string | null;
} | null;
createdAt: string;
updatedAt: string;
};
@Injectable({
providedIn: \\"root\\"
})
export class APIService {
async GetBlog(id: string): Promise<GetBlogQuery> {
const statement = \`query GetBlog($id: ID!) {
getBlog(id: $id) {
__typename
id
name
posts {
__typename
nextToken
}
createdAt
updatedAt
}
}\`;
const gqlAPIServiceArguments: any = {
id
};
const response = (await API.graphql(
graphqlOperation(statement, gqlAPIServiceArguments)
)) as any;
return <GetBlogQuery>response.data.getBlog;
}
}
",
}
`;
exports[`generateTypes amplifyJsLibraryVersion generates angular types for v6 when value is 6 1`] = `
Object {
"api.service.ts": "/* tslint:disable */
/* eslint-disable */
// This file was automatically generated and should not be edited.
import { Injectable } from \\"@angular/core\\";
import { Client, generateClient, GraphQLResult } from \\"aws-amplify/api\\";
import { Observable } from \\"rxjs\\";
export type Blog = {
__typename: \\"Blog\\";
id: string;
name: string;
posts?: ModelPostConnection | null;
createdAt: string;
updatedAt: string;
};
export type ModelPostConnection = {
__typename: \\"ModelPostConnection\\";
items: Array<Post | null>;
nextToken?: string | null;
};
export type Post = {
__typename: \\"Post\\";
id: string;
title: string;
blog?: Blog | null;
comments?: ModelCommentConnection | null;
createdAt: string;
updatedAt: string;
blogPostsId?: string | null;
};
export type ModelCommentConnection = {
__typename: \\"ModelCommentConnection\\";
items: Array<Comment | null>;
nextToken?: string | null;
};
export type Comment = {
__typename: \\"Comment\\";
id: string;
post?: Post | null;
content: string;
createdAt: string;
updatedAt: string;
postCommentsId?: string | null;
};
export type GetBlogQuery = {
__typename: \\"Blog\\";
id: string;
name: string;
posts?: {
__typename: \\"ModelPostConnection\\";
nextToken?: string | null;
} | null;
createdAt: string;
updatedAt: string;
};
@Injectable({
providedIn: \\"root\\"
})
export class APIService {
public client: Client;
constructor() {
this.client = generateClient();
}
async GetBlog(id: string): Promise<GetBlogQuery> {
const statement = \`query GetBlog($id: ID!) {
getBlog(id: $id) {
__typename
id
name
posts {
__typename
nextToken
}
createdAt
updatedAt
}
}\`;
const gqlAPIServiceArguments: any = {
id
};
const response = (await this.client.graphql({
query: statement,
variables: gqlAPIServiceArguments
})) as any;
return <GetBlogQuery>response.data.getBlog;
}
}
",
}
`;
exports[`generateTypes multipleSwiftFiles generates multiple files 1`] = `
Object {
"Types.graphql.swift": "// This file was automatically generated and should not be edited.
Expand Down
25 changes: 25 additions & 0 deletions packages/graphql-generator/src/__tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,29 @@ describe('generateTypes', () => {
expect(generateTypes(options)).rejects.toThrow('Query documents must be of type Source[] when generating multiple Swift files.');
});
});

describe('amplifyJsLibraryVersion', () => {
test('generates angular types for v5 when value is 5 or undefined', async () => {
const options: GenerateTypesOptions = {
schema: sdlSchema,
queries,
target: 'angular',
amplifyJsLibraryVersion: 5,
};
const typesV5 = await generateTypes(options);
const typesUndefined = await generateTypes({ ...options, amplifyJsLibraryVersion: undefined });
expect(typesV5).toEqual(typesUndefined);
expect(typesV5).toMatchSnapshot();
});
test('generates angular types for v6 when value is 6', async () => {
const options: GenerateTypesOptions = {
schema: sdlSchema,
queries,
target: 'angular',
amplifyJsLibraryVersion: 6,
};
const types = await generateTypes(options);
expect(types).toMatchSnapshot();
});
});
});

0 comments on commit 7b11ab7

Please sign in to comment.