Skip to content

Commit

Permalink
test(graphql-model-transformer): graphql model transformer tests (aws…
Browse files Browse the repository at this point in the history
…-amplify#7836)

* Add .circleci/config.yml

* test: ported @model v1 test suite to v2

* chore(graphql-model-transformer): removed improvePluralization check, V2 uses improvePluralization

* chore(graphql-model-transformer): added parse() to tests that generate a valid schema

* test(graphql-model-transformer): added validateModelSchema check to existing model v2 tests
  • Loading branch information
lazpavel authored and marcvberg committed Oct 13, 2021
1 parent 7814470 commit bb039b8
Show file tree
Hide file tree
Showing 5 changed files with 695 additions and 2 deletions.
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

0 comments on commit bb039b8

Please sign in to comment.