ontotext-platform-custom-scalars
Version:
A library of customized GraphQL scalars for Ontotext Platform
97 lines (59 loc) • 3.26 kB
JavaScript
import {describe, expect, test} from "@jest/globals";
import GraphQLDate from "../../src/scalars/time/GraphQLDate";
import {Kind} from "graphql";
describe(`GraphQLDate`, () => {
describe(`serialize`, () => {
describe(`valid`, () => {
test(`date object`, () => expect(GraphQLDate.serialize(new Date('Wed Jun 10 2020 09:42:45 GMT+0300'))).toEqual('2020-06-10'));
test(`string`, () => expect(GraphQLDate.serialize('Wed Jun 10 2020 09:42:45 GMT+0300')).toEqual('2020-06-10'));
test(`string UTC format`, () => expect(GraphQLDate.serialize('Wed, 10 Jun 2020 06:54:06 GMT')).toEqual('2020-06-10'));
test(`negative year `, () => expect(GraphQLDate.serialize('-2020-06-10')).toEqual('-2020-06-10'));
test(`Niki's weird date`, () => expect(GraphQLDate.serialize('-20000-04-01')).toEqual('-20000-04-01'));
test(`Tony's weird date`, () => expect(GraphQLDate.serialize('+159753-07-16')).toEqual('+159753-07-16'));
});
describe(`invalid`, () => {
test(`not date`, () => expect(() => GraphQLDate.serialize(new Map())).toThrow());
test(`empty string`, () => expect(() => GraphQLDate.serialize('')).toThrow());
test(`'null' value`, () => expect(() => GraphQLDate.serialize(null)).toThrow());
test(`'NaN' value`, () => expect(() => GraphQLDate.serialize(NaN)).toThrow());
test(`invalid string`, () => expect(() => GraphQLDate.serialize('date')).toThrow());
});
});
describe(`parseValue`, () => {
describe(`valid`, () => {
test(`string`, () => expect(GraphQLDate.parseValue('2020-06-10')).toEqual("2020-06-10T00:00:00.000Z"));
});
describe(`invalid`, () => {
test(`empty string`, () => expect(() => GraphQLDate.parseValue('')).toThrow());
test(`'null' value`, () => expect(() => GraphQLDate.parseValue(null)).toThrow());
test(`'NaN' value`, () => expect(() => GraphQLDate.parseValue(NaN)).toThrow());
test(`invalid date string`, () => expect(() => GraphQLDate.parseValue('Wed Jun 10 2020 09:42:45 GMT+0300')).toThrow());
});
});
describe(`parseLiteral`, () => {
describe(`valid`, () => {
test(`string`, () => expect(GraphQLDate.parseLiteral({
kind: Kind.STRING,
value: '2020-06-10'
})).toEqual("2020-06-10T00:00:00.000Z"));
});
describe(`invalid`, () => {
test(`invalid string date`, () => expect(() => GraphQLDate.parseLiteral({
kind: Kind.STRING,
value: 'Wed Jun 10 2020 09:42:45 GMT+0300'
})).toThrow());
test(`invalid node type`, () => expect(() => GraphQLDate.parseLiteral({
kind: Kind.INT,
value: 1
})).toThrow());
test(`NaN`, () => expect(() => GraphQLDate.parseLiteral({
kind: Kind.STRING,
value: 'NaN'
})).toThrow());
test(`'null'`, () => expect(() => GraphQLDate.parseLiteral({
kind: Kind.STRING,
value: 'null'
})).toThrow());
});
});
});