From 8273955316041b162c60e96892d5bd541334b8a6 Mon Sep 17 00:00:00 2001 From: Kevin Van Der Werff Date: Tue, 22 Oct 2019 15:28:11 +0200 Subject: [PATCH] :art: :white_check_mark: add tests & refactor - add test for getting all tags - add test for transforming resources - output.json -> mockOutput.json --- store/data.js | 15 +++++---------- test/{output.json => mockOutput.json} | 0 test/pure.test.js | 12 ++++++++++++ utils/pure.js | 16 ++++++++++++---- 4 files changed, 29 insertions(+), 14 deletions(-) rename test/{output.json => mockOutput.json} (100%) diff --git a/store/data.js b/store/data.js index 25f9e79..dd31fe5 100644 --- a/store/data.js +++ b/store/data.js @@ -7,18 +7,13 @@ import { partiallyIncludesElOf, tagsNotEmpty, cleanString, - addCleanTitleAndPath, + transformToResources, } from '../utils/pure' -export const state = () => { - const resourcesLens = R.lens(R.prop('resources'), R.assoc('resources')) - return { - resources: R.map(category => - R.over(resourcesLens, R.map(addCleanTitleAndPath(category.slug)), category), - categories), - tags: getAllTags(categories), - } -} +export const state = () => ({ + resources: transformToResources(categories), + tags: getAllTags(categories), +}) export const getters = { tags: R.prop('tags'), diff --git a/test/output.json b/test/mockOutput.json similarity index 100% rename from test/output.json rename to test/mockOutput.json diff --git a/test/pure.test.js b/test/pure.test.js index 45b055c..4afb9b6 100644 --- a/test/pure.test.js +++ b/test/pure.test.js @@ -1,4 +1,6 @@ import * as P from '../utils/pure.js' +import mockCategories from './mockCategories.json' +import mockOutput from './mockOutput.json' test('includesElOf 1', () => { expect(P.includesElOf([1, 2])([2])).toBeTruthy @@ -27,3 +29,13 @@ test('partiallyIncludesElOf 2', () => { test('partiallyIncludesElOf 3', () => { expect(P.partiallyIncludesElOf(['aa', 'b'], ['c'])).toBeFalsy }) + +test('get all tags', () => { + expect(P.getAllTags(mockCategories)).toStrictEqual([ + "generator", "grid", "layout", "visual tool", "animation", "educational", "beginner", "career", "ui", "ux", "illustration", "svg", "tips", "tricks", + ]) +}) + +test('transform resources', () => { + expect(P.transformToResources(mockCategories)).toStrictEqual(mockOutput) +}) \ No newline at end of file diff --git a/utils/pure.js b/utils/pure.js index b9bdcd7..bc5fe26 100644 --- a/utils/pure.js +++ b/utils/pure.js @@ -1,7 +1,7 @@ /*eslint-disable */ import * as R from 'ramda' -/// Types +/// TYPES /// // const Category = { // title: String, // slug: String, @@ -17,7 +17,7 @@ import * as R from 'ramda' // tags: [String], // } -/// Functions +/// FUNCTIONS /// // isNotEmpty [a] -> Bool export const isNotEmpty = R.compose(R.not, R.isEmpty) @@ -54,11 +54,19 @@ export const partiallyIncludesElOf = R.curry((list1, list2) => ) // addCleanTitleAndPath :: Object -> Resource -export const addCleanTitleAndPath = R.curry((slug, obj) => { +const addCleanTitleAndPath = R.curry((slug, obj) => { const cleanTitle = cleanStringAndRemoveSpaces(obj.title) return { ...obj, cleanTitle, path: `${slug}?card=${cleanTitle}`, } -}) \ No newline at end of file +}) + +// transformToResources :: [Object] -> [Resource] +export const transformToResources = categories => { + const resourcesLens = R.lens(R.prop('resources'), R.assoc('resources')) + return R.map(category => + R.over(resourcesLens, R.map(addCleanTitleAndPath(category.slug)), category), + categories) +} \ No newline at end of file