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

Commit

Permalink
Internal add tests for enhance-collection
Browse files Browse the repository at this point in the history
Closes #246
  • Loading branch information
thangngoc89 committed Mar 4, 2016
1 parent 67bef55 commit 2cbafe2
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 52 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/index.js
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')"
)
)
})
121 changes: 121 additions & 0 deletions src/enhance-collection/__tests__/filter.js
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" ] },
]
)
})
105 changes: 53 additions & 52 deletions src/enhance-collection/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import test from "ava"

import enhanceCollection from ".."

const collec = [
Expand All @@ -11,112 +10,114 @@ const collec = [
{ q: "uh" },
]

test("filter by object", (t) => {
test("sort with a field name", (t) => {
t.same(
enhanceCollection(
collec,
{
filter: { k: "ey" },
sort: "k",
}
),
[
{ q: "uh" },
{ k: "ay", q: "hu" },
{ k: "ei" },
{ k: "ey", l: "hi" },
{ k: "ey", l: [ "a", "b" ] },
],
"should filter by object { key: string }"
{ k: "eye" },
]
)
})

test("sort with a custom function", (t) => {
t.same(
enhanceCollection(
collec,
{
filter: { k: /y$/ },
sort: (a, b) => {
a = a["k"]
b = b["k"]
if (!a && !b) return 0
if (!a) return -1
if (!b) return 1
if (b > a) return -1
if (a > b) return 1
return 0
},
}
),
[
{ q: "uh" },
{ k: "ay", q: "hu" },
{ k: "ei" },
{ 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 = []
test("reverse", (t) => {
t.same(
enhanceCollection(
collec,
{
filter: (item) => item.k && item.k.indexOf("eye") + 1,
},
// console
{ warn: (msg) => messages.push(msg) },
reverse: true,
}
),
[
{ q: "uh" },
{ k: "eye" },
{ k: "ei" },
{ k: "ay", q: "hu" },
{ k: "ey", l: [ "a", "b" ] },
{ k: "ey", l: "hi" },
]
)
t.ok(messages.length > 0)
})

test("filter by string", (t) => {
test("limit", (t) => {
t.same(
enhanceCollection(
collec,
{
filter: "l",
limit: 1,
}
),
[
{ 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) => {
test("addSiblingReferences", (t) => {
const collec = [
{ k: 1 },
{ k: 2 },
{ k: 3 },
]

t.same(
enhanceCollection(
collec,
{
filters: [
{ k: /y$/ },
"l",
(item) => Array.isArray(item.l),
],
addSiblingReferences: true,
}
),
[
{ k: "ey", l: [ "a", "b" ] },
{
k: 1,
next: { k: 2 },
},
{
k: 2,
previous: { k: 1 },
next: { k: 3 },
},
{
k: 3,
previous: { k: 2 },
},
]
)
})

0 comments on commit 2cbafe2

Please sign in to comment.