gristlabs_grist-core/test/common/StringUnion.ts
Jarosław Sadziński a52d56f613 (core) Moving client and common tests to core
Summary:
- Moved /test/client and /test/common to core.
- Moved two files (CircularArray and RecentItems) from app/common to core/app/common.
- Moved resetOrg test to gen-server.
- `testrun.sh` is now invoking common and client test from core.
- Added missing packages to core's package.json (and revealed underscore as it is used in the main app).
- Removed Coord.js as it is not used anywhere.

Test Plan: Existing tests

Reviewers: paulfitz

Reviewed By: paulfitz

Subscribers: paulfitz

Differential Revision: https://phab.getgrist.com/D3590
2022-08-23 19:20:10 +02:00

60 lines
1.7 KiB
TypeScript

import {StringUnion} from 'app/common/StringUnion';
import {assert} from 'chai';
describe('StringUnion', function() {
// Create Dog type
const Dog = StringUnion(
"bulldog",
"poodle",
"greyhound"
);
type Dog = typeof Dog.type;
// Create Cat type
const Cat = StringUnion(
"siamese",
"sphynx",
"bengal"
);
type Cat = typeof Cat.type;
it('should provide check and guard functions', function() {
let dog: Dog;
let cat: Cat;
const greyhound = "greyhound";
const bengal = "bengal";
const giraffe = "giraffe";
// Use Dog check function.
dog = Dog.check(greyhound);
assert.equal(dog, greyhound);
assert.doesNotThrow(() => { dog = Dog.check(greyhound); });
assert.throws(() => { dog = Dog.check(bengal); },
`Value '"bengal"' is not assignable to type '"bulldog" | "poodle" | "greyhound"'`);
assert.throws(() => { dog = Dog.check(giraffe); },
`Value '"giraffe"' is not assignable to type '"bulldog" | "poodle" | "greyhound"'`);
// Use Cat check function.
cat = Cat.check(bengal);
assert.equal(cat, bengal);
assert.doesNotThrow(() => { cat = Cat.check(bengal); });
assert.throws(() => { cat = Cat.check(greyhound); },
`Value '"greyhound"' is not assignable to type '"siamese" | "sphynx" | "bengal"'`);
assert.throws(() => { cat = Cat.check(giraffe); },
`Value '"giraffe"' is not assignable to type '"siamese" | "sphynx" | "bengal"'`);
// Use Dog guard function.
assert.isTrue(Dog.guard(greyhound));
assert.isFalse(Dog.guard(bengal));
assert.isFalse(Dog.guard(giraffe));
// Use Cat guard function.
assert.isTrue(Cat.guard(bengal));
assert.isFalse(Cat.guard(greyhound));
assert.isFalse(Cat.guard(giraffe));
});
});