From 2cbafe2c81ac5a8c54f17d756c823103c69d8b5c Mon Sep 17 00:00:00 2001 From: Khoa Nguyen Date: Fri, 4 Mar 2016 16:32:30 +0700 Subject: [PATCH] Internal add tests for enhance-collection Closes #246 --- src/__tests__/index.js | 12 ++ src/enhance-collection/__tests__/filter.js | 121 +++++++++++++++++++++ src/enhance-collection/__tests__/index.js | 105 +++++++++--------- 3 files changed, 186 insertions(+), 52 deletions(-) create mode 100644 src/__tests__/index.js create mode 100644 src/enhance-collection/__tests__/filter.js diff --git a/src/__tests__/index.js b/src/__tests__/index.js new file mode 100644 index 000000000..6aa7af03c --- /dev/null +++ b/src/__tests__/index.js @@ -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')" + ) + ) +}) diff --git a/src/enhance-collection/__tests__/filter.js b/src/enhance-collection/__tests__/filter.js new file mode 100644 index 000000000..f4c2127ea --- /dev/null +++ b/src/enhance-collection/__tests__/filter.js @@ -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" ] }, + ] + ) +}) diff --git a/src/enhance-collection/__tests__/index.js b/src/enhance-collection/__tests__/index.js index 22c4c9d28..157c0cdb9 100644 --- a/src/enhance-collection/__tests__/index.js +++ b/src/enhance-collection/__tests__/index.js @@ -1,5 +1,4 @@ import test from "ava" - import enhanceCollection from ".." const collec = [ @@ -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 }, + }, ] ) })