diff --git a/package.json b/package.json index 897a67e3..21d378f9 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "start": "webpack-dev-server --config webpack.dev.js", - "test": "NODE_ENV=test mocha --recursive --compilers js:babel-register --require ./test/setup.js", + "test": "NODE_ENV=test jest", "test:watch": "npm test -- --watch", "build": "webpack --config webpack.config.js", "storybook": "start-storybook -p 9001", @@ -39,7 +39,6 @@ "babel-plugin-transform-object-rest-spread": "^6.5.0", "babel-preset-es2015": "^6.5.0", "babel-preset-react": "^6.5.0", - "babel-register": "^6.5.2", "babel-runtime": "^6.5.0", "css-loader": "^0.23.1", "eslint": "^3.5.0", @@ -49,9 +48,9 @@ "file-loader": "^0.8.5", "immutable": "^3.7.6", "imports-loader": "^0.6.5", + "jest-cli": "^15.1.1", "js-yaml": "^3.5.3", "lint-staged": "^3.0.2", - "mocha": "^2.4.5", "moment": "^2.11.2", "node-sass": "^3.10.0", "normalizr": "^2.0.0", diff --git a/src/actions/entries.js b/src/actions/entries.js index 9c9cffd6..83cb5c65 100644 --- a/src/actions/entries.js +++ b/src/actions/entries.js @@ -24,8 +24,9 @@ export const ENTRY_PERSIST_FAILURE = 'ENTRY_PERSIST_FAILURE'; /* * Simple Action Creators (Internal) + * We still need to export them for tests */ -function entryLoading(collection, slug) { +export function entryLoading(collection, slug) { return { type: ENTRY_REQUEST, payload: { @@ -35,7 +36,7 @@ function entryLoading(collection, slug) { }; } -function entryLoaded(collection, entry) { +export function entryLoaded(collection, entry) { return { type: ENTRY_SUCCESS, payload: { @@ -45,7 +46,7 @@ function entryLoaded(collection, entry) { }; } -function entriesLoading(collection) { +export function entriesLoading(collection) { return { type: ENTRIES_REQUEST, payload: { @@ -54,7 +55,7 @@ function entriesLoading(collection) { }; } -function entriesLoaded(collection, entries, pagination) { +export function entriesLoaded(collection, entries, pagination) { return { type: ENTRIES_SUCCESS, payload: { @@ -65,7 +66,7 @@ function entriesLoaded(collection, entries, pagination) { }; } -function entriesFailed(collection, error) { +export function entriesFailed(collection, error) { return { type: ENTRIES_FAILURE, error: 'Failed to load entries', @@ -74,7 +75,7 @@ function entriesFailed(collection, error) { }; } -function entryPersisting(collection, entry) { +export function entryPersisting(collection, entry) { return { type: ENTRY_PERSIST_REQUEST, payload: { @@ -84,7 +85,7 @@ function entryPersisting(collection, entry) { }; } -function entryPersisted(collection, entry) { +export function entryPersisted(collection, entry) { return { type: ENTRY_PERSIST_SUCCESS, payload: { @@ -94,7 +95,7 @@ function entryPersisted(collection, entry) { }; } -function entryPersistFail(collection, entry, error) { +export function entryPersistFail(collection, entry, error) { return { type: ENTRIES_FAILURE, error: 'Failed to persist entry', @@ -102,7 +103,7 @@ function entryPersistFail(collection, entry, error) { }; } -function emmptyDraftCreated(entry) { +export function emmptyDraftCreated(entry) { return { type: DRAFT_CREATE_EMPTY, payload: entry diff --git a/test/reducers/auth.spec.js b/src/reducers/__tests__/auth.spec.js similarity index 86% rename from test/reducers/auth.spec.js rename to src/reducers/__tests__/auth.spec.js index 83830fd5..8cc9b395 100644 --- a/test/reducers/auth.spec.js +++ b/src/reducers/__tests__/auth.spec.js @@ -1,7 +1,7 @@ import expect from 'expect'; import Immutable from 'immutable'; -import { authenticating, authenticate, authError } from '../../src/actions/auth'; -import { auth } from '../../src/reducers/auth'; +import { authenticating, authenticate, authError } from '../../actions/auth'; +import auth from '../auth'; describe('auth', () => { it('should handle an empty state', () => { diff --git a/src/reducers/__tests__/collections.spec.js b/src/reducers/__tests__/collections.spec.js new file mode 100644 index 00000000..98920d94 --- /dev/null +++ b/src/reducers/__tests__/collections.spec.js @@ -0,0 +1,26 @@ +import expect from 'expect'; +import { OrderedMap, fromJS } from 'immutable'; +import { configLoaded } from '../../actions/config'; +import collections from '../collections'; + +describe('collections', () => { + it('should handle an empty state', () => { + expect( + collections(undefined, {}) + ).toEqual( + null + ); + }); + + it('should load the collections from the config', () => { + expect( + collections(undefined, configLoaded({ collections: [ + { name: 'posts', folder: '_posts', fields: [{ name: 'title', widget: 'string' }] } + ] })) + ).toEqual( + OrderedMap({ + posts: fromJS({ name: 'posts', folder: '_posts', fields: [{ name: 'title', widget: 'string' }] }) + }) + ); + }); +}); diff --git a/test/reducers/config.spec.js b/src/reducers/__tests__/config.spec.js similarity index 92% rename from test/reducers/config.spec.js rename to src/reducers/__tests__/config.spec.js index 9167e542..5639d92f 100644 --- a/test/reducers/config.spec.js +++ b/src/reducers/__tests__/config.spec.js @@ -1,7 +1,7 @@ import expect from 'expect'; import Immutable from 'immutable'; -import { configLoaded, configLoading, configFailed } from '../../src/actions/config'; -import { config } from '../../src/reducers/config'; +import { configLoaded, configLoading, configFailed } from '../../actions/config'; +import config from '../config'; describe('config', () => { it('should handle an empty state', () => { diff --git a/src/reducers/__tests__/entries.spec.js b/src/reducers/__tests__/entries.spec.js new file mode 100644 index 00000000..2e71da03 --- /dev/null +++ b/src/reducers/__tests__/entries.spec.js @@ -0,0 +1,47 @@ +import expect from 'expect'; +import { Map, OrderedMap, fromJS } from 'immutable'; +import { entriesLoading, entriesLoaded } from '../../actions/entries'; +import reducer from '../entries'; + +describe('entries', () => { + it('should mark entries as fetching', () => { + const state = OrderedMap({ + 'posts': Map({ name: 'posts' }) + }); + expect( + reducer(state, entriesLoading(Map({ name: 'posts' }))) + ).toEqual( + OrderedMap(fromJS({ + 'posts': { name: 'posts' }, + 'pages': { + 'posts': { isFetching: true } + } + })) + ); + }); + + it('should handle loaded entries', () => { + const state = OrderedMap({ + 'posts': Map({ name: 'posts' }) + }); + const entries = [{ slug: 'a', path: '' }, { slug: 'b', title: 'B' }]; + expect( + reducer(state, entriesLoaded(Map({ name: 'posts' }), entries)) + ).toEqual( + OrderedMap(fromJS( + { + posts: { name: 'posts' }, + entities: { + 'posts.a': { slug: 'a', path: '', isFetching: false }, + 'posts.b': { slug: 'b', title: 'B', isFetching: false }, + }, + pages: { + posts: { + ids: ['a', 'b'] + } + } + } + )) + ); + }); +}); diff --git a/test/reducers/collections.spec.js b/test/reducers/collections.spec.js deleted file mode 100644 index 18ba43d0..00000000 --- a/test/reducers/collections.spec.js +++ /dev/null @@ -1,54 +0,0 @@ -import expect from 'expect'; -import { Map, OrderedMap, fromJS } from 'immutable'; -import { configLoaded } from '../../src/actions/config'; -import { entriesLoading, entriesLoaded } from '../../src/actions/entries'; -import { collections } from '../../src/reducers/collections'; - -describe('collections', () => { - it('should handle an empty state', () => { - expect( - collections(undefined, {}) - ).toEqual( - null - ); - }); - - it('should load the collections from the config', () => { - expect( - collections(undefined, configLoaded({ collections: [ - { name: 'posts', folder: '_posts', fields: [{ name: 'title', widget: 'string' }] } - ] })) - ).toEqual( - OrderedMap({ - posts: fromJS({ name: 'posts', folder: '_posts', fields: [{ name: 'title', widget: 'string' }] }) - }) - ); - }); - - it('should mark entries as loading', () => { - const state = OrderedMap({ - 'posts': Map({ name: 'posts' }) - }); - expect( - collections(state, entriesLoading(Map({ name: 'posts' }))) - ).toEqual( - OrderedMap({ - 'posts': Map({ name: 'posts', isFetching: true }) - }) - ); - }); - - it('should handle loaded entries', () => { - const state = OrderedMap({ - 'posts': Map({ name: 'posts' }) - }); - const entries = [{ slug: 'a', path: '' }, { slug: 'b', title: 'B' }]; - expect( - collections(state, entriesLoaded(Map({ name: 'posts' }), entries)) - ).toEqual( - OrderedMap({ - 'posts': fromJS({ name: 'posts', entries: entries }) - }) - ); - }); -}); diff --git a/test/setup.js b/test/setup.js deleted file mode 100644 index e69de29b..00000000 diff --git a/wallaby.config.js b/wallaby.config.js new file mode 100644 index 00000000..bf73937d --- /dev/null +++ b/wallaby.config.js @@ -0,0 +1,23 @@ +process.env.BABEL_ENV = 'test'; + +module.exports = wallaby => ({ + files: [ + { pattern: 'src/**/*.js' }, + { pattern: 'src/**/*.spec.js', ignore: true } + ], + + tests: [ + { pattern: 'src/**/*.spec.js' } + ], + + compilers: { + 'src/**/*.js': wallaby.compilers.babel() + }, + + env: { + type: 'node', + runner: 'node' + }, + + testFramework: 'jest' +});