Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(graphql-model-transformer): graphql model transformer tests #7836

Merged
merged 16 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
'<rootDir>/packages/amplify-graphql-function-transformer',
'<rootDir>/packages/amplify-graphql-http-transformer',
'<rootDir>/packages/amplify-graphql-index-transformer',
'<rootDir>/packages/amplify-graphql-model-transformer',
'<rootDir>/packages/amplify-graphql-predictions-transformer',
'<rootDir>/packages/amplify-graphql-searchable-transformer',
'<rootDir>/packages/amplify-graphql-types-generator',
Expand Down
6 changes: 4 additions & 2 deletions packages/amplify-graphql-model-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf ./lib"
"clean": "rimraf ./lib",
"test": "jest",
"test-watch": "jest --watch"
},
"dependencies": {
"@aws-amplify/graphql-transformer-core": "0.8.0",
Expand Down Expand Up @@ -67,7 +69,7 @@
"^.+\\.tsx?$": "ts-jest"
},
"testURL": "http://localhost",
"testRegex": "(src/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testRegex": "(src/__tests__/.*\\.(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform, validateModelSchema } from '@aws-amplify/graphql-transformer-core';
import { parse } from 'graphql';
import { getFieldOnObjectType, getObjectType } from './test-utils/helpers';

describe('createdAt field tests', () => {
it('should return createdAt when there is no timestamps configuration', () => {
const doc = /* GraphQL */ `
type Post @model {
id: ID!
title: String
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});

const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'createdAt');

expect(postModelField).toBeDefined();
});

it('should return null when timestamps are set to null', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: null) {
id: ID!
title: String
}
`;
const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});

const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'createdAt');

expect(postModelField).toBeUndefined();
});

it('should return null when createdAt is set to null', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: { createdAt: null }) {
id: ID!
title: String
}
`;
const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});

const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'createdAt');

expect(postModelField).toBeUndefined();
});

it('should return createdOn when createdAt is set to createdOn', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: { createdAt: "createdOn" }) {
id: ID!
title: String
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});
const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'createdOn');

expect(postModelField).toBeDefined();
});

it('should return createdAt when createdAt is not set in timestamps', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: { updatedAt: "updatedOn" }) {
id: ID!
title: String
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});
const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'createdAt');

expect(postModelField).toBeDefined();
});
});

describe('updatedAt field tests', () => {
it('should return updatedAt when there is no timestamps configuration', () => {
const doc = /* GraphQL */ `
type Post @model {
id: ID!
title: String
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});

const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'updatedAt');

expect(postModelField).toBeDefined();
});

it('should return null for updatedAt when timestamps are set to null', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: null) {
id: ID!
title: String
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});

const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'updatedAt');

expect(postModelField).toBeUndefined();
});

it('should return null when updatedAt is set to null', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: { updatedAt: null }) {
id: ID!
title: String
}
`;
const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});

const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'updatedAt');

expect(postModelField).toBeUndefined();
});

it('should return updatedOn when updatedAt is set to updatedOn', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: { updatedAt: "updatedOn" }) {
id: ID!
title: String
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});
const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'updatedOn');

expect(postModelField).toBeDefined();
});

it('should return updatedAt when updatedAt is not set in timestamps', () => {
const doc = /* GraphQL */ `
type Post @model(timestamps: { createdAt: "createdOnOn" }) {
id: ID!
title: String
}
`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer()],
});
const out = transformer.transform(doc);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);
const postModelObject = getObjectType(parsed, 'Post');
const postModelField = getFieldOnObjectType(postModelObject!, 'updatedAt');

expect(postModelField).toBeDefined();
});
});
Loading