Skip to content

Commit

Permalink
Add type declaration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkunz committed Dec 6, 2021
1 parent ae27df4 commit 076b228
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
22 changes: 21 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
},
"types": "index.d.ts",
"scripts": {
"test": "mocha"
"test": "npm run test:units && npm run test:types",
"test:units": "mocha",
"test:types": "tsc --noEmit test/*.ts"
},
"engines": {
"node": ">=14.0.0",
Expand All @@ -37,6 +39,7 @@
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^9.1.3",
"sinon": "^12.0.1"
"sinon": "^12.0.1",
"typescript": "^4.5.2"
}
}
79 changes: 79 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Verify type declarations found in index.d.ts by running:
* $ npx tsc --noEmit test/types.ts
*/
import fsm from '../index.js';

// @ts-expect-error fsm expects 2 arguments (0 provided)
const invalid1 = fsm();
// @ts-expect-error fsm expects 2 arguments (1 provided)
const invalid2 = fsm('foo');
// @ts-expect-error fsm expects string or symbol for initial state (null provided)
const invalid3 = fsm(null, {});
// @ts-expect-error fsm expects string or symbol for initial state (number provided)
const invalid4 = fsm(1, {});
// @ts-expect-error fsm expects object for states (string provided)
const invalid5 = fsm('foo', 'bar');
// @ts-expect-error fsm expects initial state to match a defined state or fallback
const invalid6 = fsm('foo', {});

const invalid7 = fsm('foo', {
foo: {
// @ts-expect-error state expects action to be string or function (object provided)
bar: {},
// @ts-expect-error state expects action to be string or function (number provided)
baz: 1,
// @ts-expect-error state expects lifecycle action to be function (string provided)
_enter: 'bar'
}
});

// A simple, valid state machine
const valid1 = fsm('off', {
off: {
toggle: 'on'
},
on: {
toggle() {
return 'off';
}
}
});

// @ts-expect-error subscribe expects callback
valid1.subscribe();
const unsub = valid1.subscribe(() => {});
// @ts-expect-error unsubscribe expects no argumernts
unsub('foo');
unsub();

// @ts-expect-error state machine expects valid event invocation
valid1.noSuchAction();

// @ts-expect-error toggle expects no arguments (1 provided)
valid1.toggle(1);
valid1.toggle();

// A state machine with fallback state (any initial state permitted)
const valid2 = fsm('initial', {
'*': {
foo: () => {}
},
});
valid2.foo();

// A state machine with overloaded action signatures
const valid3 = fsm('initial', {
'*': {
overloaded(one) {}
},
'foo': {
overloaded(one, two) {}
}
});
// @ts-expect-error overloaded expects 1 or 2 args (0 provided)
valid3.overloaded();
valid3.overloaded(1);
valid3.overloaded(1, 2);
// @ts-expect-error overloaded expects 1 or 2 args (3 provided)
valid3.overloaded(1, 2, 3);
2 changes: 1 addition & 1 deletion test.js → test/units.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from 'chai';
import sinon from 'sinon';
import fsm from './index.js';
import fsm from '../index.js';

sinon.assert.expose(assert, { prefix: '' });

Expand Down

0 comments on commit 076b228

Please sign in to comment.