2021-04-16 04:34:29 +00:00
|
|
|
import test from 'ava';
|
|
|
|
|
import GraphQL from 'graphql';
|
2017-11-07 05:54:56 +00:00
|
|
|
import {
|
|
|
|
|
Duration,
|
|
|
|
|
Locale,
|
|
|
|
|
MBID,
|
|
|
|
|
ISWC,
|
2021-04-16 04:34:29 +00:00
|
|
|
URLString,
|
|
|
|
|
} from '../../src/types/scalars.js';
|
2016-12-13 08:32:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
const { Kind } = GraphQL;
|
2016-12-13 08:32:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('Locale scalar allows language code', (t) => {
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.STRING, value: 'en' }), 'en');
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.STRING, value: 'jp' }), 'jp');
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.STRING, value: 'es' }), 'es');
|
|
|
|
|
});
|
2016-12-13 08:32:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('Locale scalar allows language and country code', (t) => {
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.STRING, value: 'en_US' }), 'en_US');
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.STRING, value: 'en_GB' }), 'en_GB');
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.STRING, value: 'fr_FR' }), 'fr_FR');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Locale scalar allows language, country, and encoding', (t) => {
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
Locale.parseLiteral({ kind: Kind.STRING, value: 'en_US.UTF-8' }),
|
|
|
|
|
'en_US.UTF-8'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
Locale.parseLiteral({ kind: Kind.STRING, value: 'de_CH.utf8' }),
|
|
|
|
|
'de_CH.utf8'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
Locale.parseLiteral({ kind: Kind.STRING, value: 'zh_TW.Big5' }),
|
|
|
|
|
'zh_TW.Big5'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
|
|
|
|
});
|
2016-12-13 08:32:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('Locale scalar only accepts strings', (t) => {
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.INT, value: 5 }), undefined);
|
|
|
|
|
t.is(Locale.parseLiteral({ kind: Kind.ENUM, value: 'xx' }), undefined);
|
|
|
|
|
});
|
2016-12-13 08:32:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('Locale scalar rejects malformed locales', (t) => {
|
|
|
|
|
t.throws(() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en_' }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
|
|
|
|
t.throws(() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en_USA' }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
|
|
|
|
t.throws(() => Locale.parseLiteral({ kind: Kind.STRING, value: 'EN' }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
|
|
|
|
t.throws(() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en_us' }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
|
|
|
|
t.throws(() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en-US' }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en_US_foo' }),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en_US-utf8' }),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
|
|
|
|
t.throws(() => Locale.parseLiteral({ kind: Kind.STRING, value: '12_US' }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
|
|
|
|
t.throws(() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en_US.' }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() => Locale.parseLiteral({ kind: Kind.STRING, value: 'en_US.utf!' }),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
|
|
|
|
});
|
2016-12-13 08:32:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('Duration scalar must be a positive integer', (t) => {
|
|
|
|
|
t.is(Duration.parseLiteral({ kind: Kind.INT, value: 0 }), 0);
|
|
|
|
|
t.is(Duration.parseLiteral({ kind: Kind.INT, value: 1 }), 1);
|
|
|
|
|
t.is(Duration.parseLiteral({ kind: Kind.INT, value: 3000 }), 3000);
|
|
|
|
|
t.is(Duration.parseLiteral({ kind: Kind.STRING, value: '1000' }), undefined);
|
|
|
|
|
t.throws(() => Duration.parseLiteral({ kind: Kind.INT, value: -1 }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
|
|
|
|
t.throws(() => Duration.parseLiteral({ kind: Kind.INT, value: -1000 }), {
|
|
|
|
|
instanceOf: TypeError,
|
|
|
|
|
});
|
|
|
|
|
t.is(Duration.parseValue(0), 0);
|
|
|
|
|
t.is(Duration.parseValue(1), 1);
|
|
|
|
|
t.is(Duration.parseValue(3000), 3000);
|
|
|
|
|
t.throws(() => Duration.parseValue(-1), { instanceOf: TypeError });
|
|
|
|
|
t.throws(() => Duration.parseValue(-1000), { instanceOf: TypeError });
|
|
|
|
|
});
|
2016-12-13 08:32:21 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('URLString scalar must be a valid URL', (t) => {
|
|
|
|
|
t.is(URLString.parseLiteral({ kind: Kind.INT, value: 1000 }), undefined);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
URLString.parseLiteral({
|
|
|
|
|
kind: Kind.STRING,
|
2021-04-16 04:34:29 +00:00
|
|
|
value: 'http://www.google.com',
|
2017-11-07 05:54:56 +00:00
|
|
|
}),
|
|
|
|
|
'http://www.google.com'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() => URLString.parseLiteral({ kind: Kind.STRING, value: 'foo:bar' }),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() => URLString.parseLiteral({ kind: Kind.STRING, value: 'foo:/bar' }),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() => URLString.parseLiteral({ kind: Kind.STRING, value: 'foo://bar' }),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() => URLString.parseLiteral({ kind: Kind.STRING, value: 'foo://bar.' }),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
|
|
|
|
});
|
2016-12-14 04:05:35 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('ISWC scalar only accepts strings', (t) => {
|
|
|
|
|
t.is(ISWC.parseLiteral({ kind: Kind.STRING, value: 'foo' }), 'foo');
|
|
|
|
|
t.is(ISWC.parseLiteral({ kind: Kind.INT, value: 5 }), undefined);
|
|
|
|
|
t.is(ISWC.parseLiteral({ kind: Kind.ENUM, value: 'xx' }), undefined);
|
|
|
|
|
});
|
2016-12-14 04:05:35 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('ISWC scalar can be any string', (t) => {
|
|
|
|
|
t.is(ISWC.parseValue('foo'), 'foo');
|
|
|
|
|
t.is(ISWC.parseValue('123-456'), '123-456');
|
|
|
|
|
t.is(ISWC.parseValue('!@#$%^&*'), '!@#$%^&*');
|
|
|
|
|
});
|
2016-12-20 05:21:07 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('MBID scalar only accepts strings', (t) => {
|
|
|
|
|
t.is(MBID.parseLiteral({ kind: Kind.INT, value: 12345 }), undefined);
|
|
|
|
|
t.is(MBID.parseLiteral({ kind: Kind.ENUM, value: 'xx' }), undefined);
|
|
|
|
|
});
|
2016-12-14 04:05:35 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('MBID scalar must be a valid UUID', (t) => {
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
MBID.parseLiteral({
|
|
|
|
|
kind: Kind.STRING,
|
2021-04-16 04:34:29 +00:00
|
|
|
value: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8',
|
2017-11-07 05:54:56 +00:00
|
|
|
}),
|
|
|
|
|
'c8da2e40-bd28-4d4e-813a-bd2f51958ba8'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
2017-11-07 05:54:56 +00:00
|
|
|
t.throws(
|
|
|
|
|
() =>
|
|
|
|
|
MBID.parseLiteral({
|
|
|
|
|
kind: Kind.STRING,
|
2021-04-16 04:34:29 +00:00
|
|
|
value: 'c8da2e40-bd28-4d4e-813a-bd2f51958bag',
|
2017-11-07 05:54:56 +00:00
|
|
|
}),
|
2021-04-16 04:34:29 +00:00
|
|
|
{ instanceOf: TypeError }
|
|
|
|
|
);
|
|
|
|
|
});
|