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

More type tests #19947

Merged
merged 13 commits into from
Feb 18, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { assertDestroyablesDestroyed } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

if (assertDestroyablesDestroyed) {
chriskrycho marked this conversation as resolved.
Show resolved Hide resolved
expectTypeOf(assertDestroyablesDestroyed()).toEqualTypeOf<void>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { associateDestroyableChild } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

class Foo {}
class Bar {}

let foo = new Foo();
let bar = new Bar();

expectTypeOf(associateDestroyableChild(foo, bar)).toEqualTypeOf<Bar>();
// @ts-expect-error number is not destroyable
associateDestroyableChild(1, bar);
// @ts-expect-error number is not destroyable
associateDestroyableChild(foo, 1);
// @ts-expect-error two values required
associateDestroyableChild(foo);
15 changes: 15 additions & 0 deletions packages/@ember/destroyable/type-tests/destroy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { destroy, registerDestructor } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

let obj = {};

registerDestructor(obj, () => {
/* Will get called when destroyed */
});

expectTypeOf(destroy(obj)).toEqualTypeOf<void>();

// @ts-expect-error not destroyable
destroy(1);
// @ts-expect-error requires arg
destroy();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { enableDestroyableTracking } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

if (enableDestroyableTracking) {
chriskrycho marked this conversation as resolved.
Show resolved Hide resolved
expectTypeOf(enableDestroyableTracking()).toEqualTypeOf<void>();
}
16 changes: 16 additions & 0 deletions packages/@ember/destroyable/type-tests/is-destroyed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { destroy, isDestroyed } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

let obj = {};

expectTypeOf(isDestroyed(obj)).toEqualTypeOf<boolean>(); // false
destroy(obj);

// ...sometime later, after scheduled destruction

expectTypeOf(isDestroyed(obj)).toEqualTypeOf<boolean>(); // true

// @ts-expect-error requires a destroyable value
isDestroyed(1);
// @ts-expect-error requires a value
isDestroyed();
19 changes: 19 additions & 0 deletions packages/@ember/destroyable/type-tests/is-destroying.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { destroy, isDestroying, isDestroyed } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

let obj = {};

expectTypeOf(isDestroying(obj)).toEqualTypeOf<boolean>(); // false

destroy(obj);

isDestroying(obj); // true

// ...sometime later, after scheduled destruction
isDestroyed(obj); // true
isDestroying(obj); // true

// @ts-expect-error requires a destroyable value
isDestroying(1);
// @ts-expect-error requires a value
isDestroying();
27 changes: 27 additions & 0 deletions packages/@ember/destroyable/type-tests/register-destructor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { destroy, registerDestructor } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

class Foo {}
let obj = new Foo();

expectTypeOf(
registerDestructor(obj, () => {
/* Will get called when destroyed */
})
).toEqualTypeOf<(destroyable: {}) => void>();

registerDestructor(obj, (_obj: Foo) => {});

destroy(obj);

// @ts-expect-error not destroyable
registerDestructor(1, () => {});

// @ts-expect-error requires object
registerDestructor(() => {});

// @ts-expect-error requires callback
registerDestructor(obj);

// @ts-expect-error invalid callback
registerDestructor(obj, (blah: number) => {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { registerDestructor, unregisterDestructor } from '@ember/destroyable';
import { expectTypeOf } from 'expect-type';

class Foo {
type: 'foo' = 'foo';
}
let obj = new Foo();

let destructor = registerDestructor(obj, () => {
/* Will get called when destroyed */
});

expectTypeOf(unregisterDestructor(obj, destructor)).toEqualTypeOf<void>();

// @ts-expect-error invalid destructor
unregisterDestructor(obj, 1);

// @ts-expect-error requires destructor
unregisterDestructor(obj);

// @ts-expect-error requires object
unregisterDestructor(destructor);

class Bar {
type: 'blah' = 'blah';
}
let obj2 = new Bar();

// @ts-expect-error destroyable type mismatch
unregisterDestructor(obj2, destructor);