test with ts

This commit is contained in:
Yanick Champoux 2021-10-16 11:48:03 -04:00
parent c5a9a7397a
commit 7cd7e27805

View File

@ -1,10 +1,7 @@
import { test } from 'tap';
import sinon from 'sinon';
import { Updux } from './Updux';
import { action } from './actions';
import { Updux } from './Updux.js';
import { action } from './actions.js';
test('basic state', async (t) => {
test('basic state', () => {
const alpha = new Updux({
initial: { sub: 1 },
});
@ -18,7 +15,7 @@ test('basic state', async (t) => {
subduxes: { alpha, beta },
});
t.same(dux.initial, {
expect(dux.initial).toMatchObject({
a: 1,
b: 'two',
alpha: { sub: 1 },
@ -26,7 +23,7 @@ test('basic state', async (t) => {
});
});
test('basic actions', async (t) => {
test('basic actions', () => {
const alpha = new Updux({
actions: {
foo: action('foo'),
@ -42,10 +39,10 @@ test('basic actions', async (t) => {
},
});
t.same(Object.keys(dux.actions).sort(), ['bar', 'foo']);
expect(Object.keys(dux.actions).sort()).toMatchObject(['bar', 'foo']);
});
test('setAction', async (t) => {
test('setAction', () => {
const dux = new Updux({
actions: {
bar: action('bar'),
@ -53,10 +50,10 @@ test('setAction', async (t) => {
});
dux.setAction('foo');
t.same(Object.keys(dux.actions).sort(), ['bar', 'foo']);
expect(Object.keys(dux.actions).sort()).toMatchObject(['bar', 'foo']);
});
test('basic selectors', async (t) => {
test('basic selectors', () => {
const alpha = new Updux({
initial: { quux: 3 },
selectors: {
@ -83,20 +80,20 @@ test('basic selectors', async (t) => {
);
dux.setAction('stuff');
t.equal(dux.selectors.getBar({ bar: 3 }), 3);
t.equal(dux.selectors.getFoo({ foo: 3 }), 3);
expect(dux.selectors.getBar({ bar: 3 })).toEqual(3);
expect(dux.selectors.getFoo({ foo: 3 })).toEqual(3);
t.equal(alpha.selectors.getQuux({ quux: 1 }), 1);
t.equal(dux.selectors.getQuux({ alpha: { quux: 1 } }), 1);
expect(alpha.selectors.getQuux({ quux: 1 })).toEqual(1);
expect(dux.selectors.getQuux({ alpha: { quux: 1 } })).toEqual(1);
const store = dux.createStore();
t.equal(store.getState.getFoo(), 1);
t.equal(store.getState.getQuux(), 3);
t.equal(store.getState.getAdd(7), 8);
expect(store.getState.getFoo()).toEqual(1);
expect(store.getState.getQuux()).toEqual(3);
expect(store.getState.getAdd(7)).toEqual(8);
});
test('mutations', async (t) => {
test('mutations', () => {
const alpha = new Updux({
initial: { quux: 3 },
});
@ -124,7 +121,7 @@ test('mutations', async (t) => {
const store = dux.createStore();
t.same(store.getState(), {
expect(store.getState()).toMatchObject({
foo: 1,
bar: 4,
alpha: { quux: 3 },
@ -132,7 +129,7 @@ test('mutations', async (t) => {
store.dispatch.add(10);
t.same(store.getState(), {
expect(store.getState()).toMatchObject({
foo: 11,
bar: 4,
alpha: { quux: 13 },
@ -140,15 +137,15 @@ test('mutations', async (t) => {
store.dispatch.subtract(20);
t.same(store.getState(), {
expect(store.getState()).toMatchObject({
foo: 11,
bar: -16,
alpha: { quux: 13 },
});
});
test('middleware', async (t) => {
const fooEffect = sinon.fake.returns(true);
test('middleware', () => {
const fooEffect = jest.fn(() => true);
const dux = new Updux({
effects: {
@ -161,9 +158,9 @@ test('middleware', async (t) => {
const store = dux.createStore();
t.notOk(fooEffect.called, 'not called yet');
expect(fooEffect).not.toBeCalled();
store.dispatch({ type: 'foo' });
t.ok(fooEffect.called, "now it's been called");
expect(fooEffect).toBeCalled();
});