This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internal add tests for enhance-collection
Closes #246
- Loading branch information
1 parent
67bef55
commit 2cbafe2
Showing
3 changed files
with
186 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import test from "ava" | ||
|
||
test("should throw if trying to require statinamic directly", (t) => { | ||
t.throws( | ||
() => { | ||
require("..") | ||
}, | ||
(error) => error.message.includes( | ||
"Please use submodules directly (e.g.: 'statinamic/lib/client')" | ||
) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import test from "ava" | ||
import enhanceCollection from ".." | ||
|
||
const collec = [ | ||
{ k: "ey", l: "hi" }, | ||
{ k: "ey", l: [ "a", "b" ] }, | ||
{ k: "ay", q: "hu" }, | ||
{ k: "ei" }, | ||
{ k: "eye" }, | ||
{ q: "uh" }, | ||
] | ||
|
||
test("filter by object", (t) => { | ||
t.same( | ||
enhanceCollection( | ||
collec, | ||
{ | ||
filter: { k: "ey" }, | ||
} | ||
), | ||
[ | ||
{ k: "ey", l: "hi" }, | ||
{ k: "ey", l: [ "a", "b" ] }, | ||
], | ||
"should filter by object { key: string }" | ||
) | ||
|
||
t.same( | ||
enhanceCollection( | ||
collec, | ||
{ | ||
filter: { k: /y$/ }, | ||
} | ||
), | ||
[ | ||
{ k: "ey", l: "hi" }, | ||
{ k: "ey", l: [ "a", "b" ] }, | ||
{ k: "ay", q: "hu" }, | ||
], | ||
"should filter by object { key: regexp }" | ||
) | ||
}) | ||
|
||
test("filter by custom function", (t) => { | ||
t.same( | ||
enhanceCollection( | ||
collec, | ||
{ | ||
filter: (item) => item.k === "eye", | ||
} | ||
), | ||
[ | ||
{ k: "eye" }, | ||
] | ||
) | ||
}) | ||
|
||
test("filter by custom function (and warn if not boolean)", (t) => { | ||
const messages = [] | ||
t.same( | ||
enhanceCollection( | ||
collec, | ||
{ | ||
filter: (item) => item.k && item.k.indexOf("eye") + 1, | ||
}, | ||
// console | ||
{ warn: (msg) => messages.push(msg) }, | ||
), | ||
[ | ||
{ k: "eye" }, | ||
] | ||
) | ||
t.ok(messages.length > 0) | ||
}) | ||
|
||
test("filter by string", (t) => { | ||
t.same( | ||
enhanceCollection( | ||
collec, | ||
{ | ||
filter: "l", | ||
} | ||
), | ||
[ | ||
{ k: "ey", l: "hi" }, | ||
{ k: "ey", l: [ "a", "b" ] }, | ||
], | ||
) | ||
}) | ||
|
||
test("multiple filters", (t) => { | ||
t.same( | ||
enhanceCollection( | ||
collec, | ||
{ | ||
filters: [ "q", "k" ], | ||
} | ||
), | ||
[ | ||
{ k: "ay", q: "hu" }, | ||
] | ||
) | ||
}) | ||
|
||
test("mix filters ", (t) => { | ||
t.same( | ||
enhanceCollection( | ||
collec, | ||
{ | ||
filters: [ | ||
{ k: /y$/ }, | ||
"l", | ||
(item) => Array.isArray(item.l), | ||
], | ||
} | ||
), | ||
[ | ||
{ k: "ey", l: [ "a", "b" ] }, | ||
] | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters