Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Commit

Permalink
test: add main tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Mar 24, 2020
1 parent a86e080 commit 812f4bc
Show file tree
Hide file tree
Showing 3 changed files with 325 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -30,6 +30,7 @@
"@rollup/plugin-commonjs": "^11.0.1",
"@rollup/plugin-node-resolve": "^7.0.0",
"@rollup/plugin-strip": "^1.3.1",
"@types/jest": "^25.1.4",
"@types/react": "^16.9.17",
"@typescript-eslint/eslint-plugin": "^2.16.0",
"@typescript-eslint/parser": "^2.16.0",
Expand Down
316 changes: 316 additions & 0 deletions tests/main.test.ts
@@ -0,0 +1,316 @@
import 'regenerator-runtime/runtime';
import { createStore, createEvent, createEffect } from 'effector';
import { createThrottle } from '../src';

const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

describe('event', () => {
test('throttle event', async () => {
const watcher = jest.fn();

const trigger = createEvent();
const throttled = createThrottle(trigger, 40);

throttled.watch(watcher);

trigger();
trigger();
trigger();

expect(watcher).not.toBeCalled();

await wait(40);

expect(watcher).toBeCalledTimes(1);
});

test('throttled event with wait', async () => {
const watcher = jest.fn();

const trigger = createEvent();
const throttled = createThrottle(trigger, 40);

throttled.watch(watcher);

trigger();

await wait(30);
trigger();

await wait(30);
trigger();

expect(watcher).toBeCalled();

await wait(40);

expect(watcher).toBeCalledTimes(2);
});

test('throttled event triggered with first value', async () => {
const watcher = jest.fn();

const trigger = createEvent<number>();
const throttled = createThrottle(trigger, 40);

throttled.watch(watcher);

trigger(0);
trigger(1);
trigger(2);

expect(watcher).not.toBeCalled();

await wait(40);

expect(watcher.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
0,
],
]
`);
});

test('throttled event works after trigger', async () => {
const watcher = jest.fn();

const trigger = createEvent<number>();
const throttled = createThrottle(trigger, 40);

throttled.watch(watcher);

trigger(0);
trigger(1);
trigger(2);

expect(watcher).not.toBeCalled();

await wait(40);

expect(watcher.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
0,
],
]
`);

trigger(3);
await wait(30);
trigger(4);
await wait(50);

expect(watcher.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
0,
],
Array [
3,
],
]
`);
});
});

describe('effect', () => {
test('throttle effect', async () => {
const watcher = jest.fn();

const trigger = createEffect<void, void>().use(() => undefined);
const throttle = createThrottle(trigger, 40);

throttle.watch(watcher);

trigger();
trigger();
trigger();

expect(watcher).not.toBeCalled();

await wait(40);

expect(watcher).toBeCalledTimes(1);
});

test('throttle effect with wait', async () => {
const watcher = jest.fn();

const trigger = createEffect<void, void>().use(() => undefined);
const throttle = createThrottle(trigger, 40);

throttle.watch(watcher);

trigger();

await wait(30);
trigger();

await wait(30);
trigger();

expect(watcher).toBeCalledTimes(1);

await wait(40);

expect(watcher).toBeCalledTimes(2);
});

test('throttle effect triggered with last value', async () => {
const watcher = jest.fn();

const trigger = createEffect<number, void>().use(() => undefined);
const throttled = createThrottle(trigger, 40);

throttled.watch(watcher);

trigger(0);
trigger(1);
trigger(2);

expect(watcher).not.toBeCalled();

await wait(40);

expect(watcher).toBeCalledTimes(1);
expect(watcher.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
0,
],
]
`);
});

test('throttled effect works after trigger', async () => {
const watcher = jest.fn();

const trigger = createEffect<number, void>().use(() => undefined);
const throttled = createThrottle(trigger, 40);

throttled.watch(watcher);

trigger(0);
trigger(1);
trigger(2);

expect(watcher).not.toBeCalled();

await wait(40);

expect(watcher).toBeCalledTimes(1);
expect(watcher).toBeCalledWith(0);

trigger(3);
await wait(30);
trigger(4);

await wait(50);

expect(watcher).toBeCalledTimes(2);
expect(watcher.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
0,
],
Array [
3,
],
]
`);
});
});

describe('store', () => {
test('throttle store and pass values', async () => {
const watcher = jest.fn();

const trigger = createEvent<number>();
const $store = createStore(0);

$store.on(trigger, (_, value) => value);

const throttled = createThrottle($store, 40);

throttled.watch(watcher);

trigger(0);
trigger(1);
trigger(2);

expect(watcher).not.toBeCalled();

await wait(40);

expect(watcher).toBeCalledTimes(1);
expect(watcher.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
1,
],
]
`);
});
});

test('debounce do not affect another instance', async () => {
const watcherFirst = jest.fn();
const triggerFirst = createEvent<number>();
const throttledFirst = createThrottle(triggerFirst, 20);
throttledFirst.watch(watcherFirst);

const watcherSecond = jest.fn();
const triggerSecond = createEvent<string>();
const throttledSecond = createThrottle(triggerSecond, 60);
throttledSecond.watch(watcherSecond);

triggerFirst(0);

expect(watcherFirst).not.toBeCalled();
await wait(20);

expect(watcherFirst).toBeCalledTimes(1);
expect(watcherFirst.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
0,
],
]
`);

expect(watcherSecond).not.toBeCalled();

triggerSecond('foo');
triggerFirst(1);
await wait(20);
triggerFirst(2);
await wait(20);

expect(watcherFirst).toBeCalledTimes(3);
expect(watcherFirst.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
0,
],
Array [
1,
],
Array [
2,
],
]
`);
expect(watcherSecond).not.toBeCalled();

await wait(20);

expect(watcherSecond).toBeCalledTimes(1);
expect(watcherSecond.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"foo",
],
]
`);
});
8 changes: 8 additions & 0 deletions yarn.lock
Expand Up @@ -1250,6 +1250,14 @@
"@types/istanbul-lib-coverage" "*"
"@types/istanbul-lib-report" "*"

"@types/jest@^25.1.4":
version "25.1.4"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-25.1.4.tgz#9e9f1e59dda86d3fd56afce71d1ea1b331f6f760"
integrity sha512-QDDY2uNAhCV7TMCITrxz+MRk1EizcsevzfeS6LykIlq2V1E5oO4wXG8V2ZEd9w7Snxeeagk46YbMgZ8ESHx3sw==
dependencies:
jest-diff "^25.1.0"
pretty-format "^25.1.0"

"@types/json-schema@^7.0.3":
version "7.0.4"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
Expand Down

0 comments on commit 812f4bc

Please sign in to comment.