Skip to content

Commit

Permalink
Merge pull request #22 from Ayc0/Ayc0/tests
Browse files Browse the repository at this point in the history
minor: use test.serial + mock
  • Loading branch information
Ayc0 authored Jan 23, 2022
2 parents cb26908 + 120759b commit 5097825
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
4 changes: 2 additions & 2 deletions tests/import.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "ava";

test("can import mock-match-media from ESM", async (t) => {
test.serial("can import mock-match-media from ESM", async (t) => {
const exportsDefault = await import("mock-match-media");
t.deepEqual(Object.keys(exportsDefault), [
"MediaQueryListEvent",
Expand All @@ -13,7 +13,7 @@ test("can import mock-match-media from ESM", async (t) => {
t.pass();
});

test("can import mock-match-media/polyfill from ESM", async (t) => {
test.serial("can import mock-match-media/polyfill from ESM", async (t) => {
t.is(global.matchMedia, undefined);
const { matchMedia } = await import("mock-match-media");

Expand Down
70 changes: 49 additions & 21 deletions tests/listeners.cjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
const test = require("ava");
const { matchMedia, setMedia, cleanupListeners, cleanupMedia, cleanup } = require("mock-match-media");

const waitFor = async (cb) => {
await cb().catch(() => {
return new Promise((res, rej) => waitFor(cb).then(res).catch(rej), 10);
});
};

test.afterEach(() => {
// cleanup listeners and state after each test
cleanup();
});

test(".addListener()", async (t) => {
const mock = () => {
const calls = [];
return [
(event) => {
calls.push(event);
},
calls,
];
};

test.serial(".addListener()", (t) => {
const mql = matchMedia("(min-width: 500px)");

const calls = [];
const cb = (event) => {
calls.push(event);
};
const [cb, calls] = mock();

mql.addListener(cb);

Expand Down Expand Up @@ -59,13 +60,10 @@ test(".addListener()", async (t) => {
t.pass();
});

test(".addEventListener()", async (t) => {
test.serial(".addEventListener()", (t) => {
const mql = matchMedia("(min-width: 500px)");

const calls = [];
const cb = (event) => {
calls.push(event);
};
const [cb, calls] = mock();

mql.addEventListener("change", cb);

Expand Down Expand Up @@ -106,13 +104,10 @@ test(".addEventListener()", async (t) => {
t.pass();
});

test("listeners get only called once when multiple features change", async (t) => {
test.serial("listeners get only called once when multiple features change", (t) => {
const mql = matchMedia("(min-width: 500px) and (min-height: 200px)");

const calls = [];
const cb = (event) => {
calls.push(event);
};
const [cb, calls] = mock();

t.is(mql.matches, false);

Expand Down Expand Up @@ -142,6 +137,39 @@ test("listeners get only called once when multiple features change", async (t) =
t.pass();
});

test.serial("ensure that when the same fn is used twice, it won't be called twice on each change", (t) => {
const mql = matchMedia("(min-width: 500px)");

const [cb, calls] = mock();

mql.addEventListener("change", cb);
mql.addEventListener("change", cb);

setMedia({
width: "600px",
});
t.is(calls.length, 1);

t.pass();
});

test.serial.skip("ensure that when the same fn is used in 2 different MQL, it will be called twice", (t) => {
const mql1 = matchMedia("(min-width: 500px)");
const mql2 = matchMedia("(min-width: 500px)");

const [cb, calls] = mock();

mql1.addEventListener("change", cb);
mql2.addEventListener("change", cb);

setMedia({
width: "600px",
});
t.is(calls.length, 2);

t.pass();
});

test.todo("check mql.onchange");
test.todo("check mql.dispatchEvent");
test.todo("check {once: true} in addEventListener");
6 changes: 3 additions & 3 deletions tests/require.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require("ava");

test("can import mock-match-media from CJS", (t) => {
test.serial("can import mock-match-media from CJS", (t) => {
const exportsDefault = require("mock-match-media");
t.deepEqual(Object.keys(exportsDefault), [
"MediaQueryListEvent",
Expand All @@ -13,7 +13,7 @@ test("can import mock-match-media from CJS", (t) => {
t.pass();
});

test("can import mock-match-media/polyfill from CJS", (t) => {
test.serial("can import mock-match-media/polyfill from CJS", (t) => {
t.is(global.matchMedia, undefined);
t.is(global.MediaQueryListEvent, undefined);
const { matchMedia, MediaQueryListEvent } = require("mock-match-media");
Expand All @@ -25,7 +25,7 @@ test("can import mock-match-media/polyfill from CJS", (t) => {
t.pass();
});

test("can import mock-match-media/jest-setup from CJS", (t) => {
test.serial("can import mock-match-media/jest-setup from CJS", (t) => {
t.is(global.matchMedia, undefined);
const { matchMedia } = require("mock-match-media");
require("mock-match-media/jest-setup");
Expand Down
6 changes: 3 additions & 3 deletions tests/simple-use.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require("ava");
const { matchMedia, setMedia, cleanupListeners, cleanupMedia, cleanup } = require("mock-match-media");

test(".matches", (t) => {
test.serial(".matches", (t) => {
const mql = matchMedia("(min-width: 500px)");

t.is(mql.matches, false);
Expand All @@ -21,7 +21,7 @@ test(".matches", (t) => {
t.pass();
});

test("cleanupMedia", (t) => {
test.serial("cleanupMedia", (t) => {
const doesMatch = () => matchMedia("(min-width: 500px)").matches;

setMedia({
Expand All @@ -33,7 +33,7 @@ test("cleanupMedia", (t) => {
t.is(doesMatch(), false);
});

test("cleanup", (t) => {
test.serial("cleanup", (t) => {
const doesMatch = () => matchMedia("(min-width: 500px)").matches;

setMedia({
Expand Down

0 comments on commit 5097825

Please sign in to comment.