Commit 2bdca5ca authored by Jure's avatar Jure

Merge branch '52-action-tests' into 'master'

Remove extra action test assertions

Closes #52

See merge request !114
parents e4a5332d aea195b0
Pipeline #2652 passed with stage
in 1 minute and 38 seconds
......@@ -18,28 +18,6 @@ describe('Collection actions', () => {
}
})
// get a list of collections, with the specified fields
describeAction('getCollections', {
firstarg: {
fields: ['type', 'title']
},
types: {
request: T.GET_COLLECTIONS_REQUEST,
success: T.GET_COLLECTIONS_SUCCESS
},
properties: {
request: ['type'],
success: ['type', 'collections', 'receivedAt'],
failure: ['type', 'error']
}
}, (action, data) => {
const filteredCollection = data.GET_COLLECTIONS_SUCCESS.collections[0]
expect(filteredCollection).toHaveProperty('id')
expect(filteredCollection).toHaveProperty('type')
expect(filteredCollection).toHaveProperty('title')
expect(filteredCollection).not.toHaveProperty('created')
})
describeAction('getCollectionTeams', {
firstarg: {id: 123},
types: {
......@@ -54,8 +32,6 @@ describe('Collection actions', () => {
}
})
let newcol
describeAction('createCollection', {
firstarg: {
type: 'testing',
......@@ -74,7 +50,7 @@ describe('Collection actions', () => {
})
describeAction('getCollection', {
firstarg: newcol,
firstarg: {id: 123},
types: {
request: T.GET_COLLECTION_REQUEST,
success: T.GET_COLLECTION_SUCCESS,
......@@ -85,13 +61,10 @@ describe('Collection actions', () => {
success: ['type', 'collection', 'receivedAt'],
failure: ['type', 'isFetching', 'collection', 'error']
}
}, (action, data) => {
const collection = data.GET_COLLECTION_SUCCESS.collection
expect(collection).toHaveProperty('id')
})
describeAction('updateCollection', {
firstarg: newcol,
firstarg: {id: 123},
secondarg: {
type: 'testing',
title: 'this is an updated collection'
......@@ -106,10 +79,6 @@ describe('Collection actions', () => {
success: ['type', 'collection'],
failure: ['type', 'isFetching', 'collection', 'error']
}
}, (action, data) => {
expect(
data.UPDATE_COLLECTION_SUCCESS.collection.title
).toBe('this is an updated collection')
})
// NOTE: enable this once PATCH method is implemented on the server
......@@ -136,7 +105,7 @@ describe('Collection actions', () => {
// })
describeAction('deleteCollection', {
firstarg: newcol,
firstarg: {id: 123},
types: {
request: T.DELETE_COLLECTION_REQUEST,
success: T.DELETE_COLLECTION_SUCCESS,
......@@ -147,9 +116,5 @@ describe('Collection actions', () => {
success: ['type', 'collection'],
failure: ['type', 'collection', 'update', 'error']
}
}, (action, data) => {
expect(
data.DELETE_COLLECTION_SUCCESS.collection.title
).toBe(newcol.collection.title)
})
})
......@@ -33,12 +33,6 @@ describe('fragments actions', () => {
properties: {
success: ['fragments']
}
}, (action, data) => {
const filteredFragment = data.GET_FRAGMENTS_SUCCESS.fragments[0]
expect(filteredFragment).toHaveProperty('id')
expect(filteredFragment).toHaveProperty('type')
expect(filteredFragment).toHaveProperty('presentation')
expect(filteredFragment).not.toHaveProperty('source')
})
describeAction('createFragment', {
......
......@@ -5,8 +5,6 @@ const describeAction = require('../helpers/describeAction')(actions)
const T = require('../../src/actions/types')
describe('teams actions', () => {
let team
describeAction('getTeams', {
types: {
request: T.GET_TEAMS_REQUEST,
......@@ -43,12 +41,10 @@ describe('teams actions', () => {
success: ['team'],
failure: ['isFetching', 'team', 'error']
}
}, (action, data) => {
team = data[T.CREATE_TEAM_SUCCESS].team
})
describeAction('updateTeam', {
firstarg: team,
firstarg: {id: 234},
secondard: {
name: 'My readers',
teamType: {
......@@ -74,7 +70,7 @@ describe('teams actions', () => {
})
describeAction('deleteTeam', {
firstarg: team,
firstarg: {id: 234},
types: {
request: T.DELETE_TEAM_REQUEST,
success: T.DELETE_TEAM_SUCCESS,
......
const defaults = require('lodash/defaults')
const allactions = require('../../src/actions').default
const api = require('../../src/helpers/api')
const empty = {}
const mockDispatch = () => {}
const mockGetState = () => {
return {
currentUser: {}
......@@ -11,16 +8,10 @@ const mockGetState = () => {
}
function mockApi (succeed = true) {
Object.keys(api).forEach(method => {
jest.spyOn(api, method).mockImplementation(() => succeed
? Promise.resolve({}) : Promise.reject(new Error({})))
})
}
function unMockApi () {
Object.keys(api).forEach(method => {
api[method].mockRestore()
})
const implementation = () => succeed ? Promise.resolve({}) : Promise.reject(new Error({}))
Object.keys(api)
.filter(method => typeof api[method] === 'function')
.forEach(method => jest.spyOn(api, method).mockImplementation(implementation))
}
// custom Jest matcher
......@@ -35,31 +26,14 @@ expect.extend({
}
})
const describeAction = actions => (key, opts, cb) => {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
defaults(opts, {
firstarg: empty,
secondarg: empty,
types: {},
properties: {}
})
const describeAction = actions => (key, opts) => {
describe(key, () => {
const data = {}
let action
// TODO this is broken because failures in the callback are not reported
if (cb) afterAll(() => cb(action, data))
const actionCreator = actions[key]
beforeEach(mockApi)
afterEach(unMockApi)
afterEach(() => jest.restoreAllMocks())
// functional tests - no server required
it('is exported from the file', () => {
expect(actions).toHaveProperty(key)
})
......@@ -68,50 +42,42 @@ const describeAction = actions => (key, opts, cb) => {
expect(allactions).toHaveProperty(key)
})
action = actions[key]
it('returns a fetcher function', () => {
const returned = action(mockDispatch, mockGetState)
expect(typeof returned).toBe('function')
const thunk = actionCreator(() => {}, mockGetState)
expect(typeof thunk).toBe('function')
})
it('returns a promise from the fetcher function', () => {
const fetcher = action(opts.firstarg, opts.secondarg)
const returned = fetcher(mockDispatch, mockGetState)
const thunk = actionCreator(opts.firstarg, opts.secondarg)
const returned = thunk(() => {}, mockGetState)
expect(typeof returned.then).toBe('function')
})
it('dispatches a typed fragment', () => {
const fetcher = action(opts.firstarg, opts.secondarg)
let frag
fetcher(fragment => { frag = fragment }, mockGetState)
expect(frag).toBeDefined()
expect(frag).toHaveProperty('type')
it('dispatches an action object with a type property', () => {
const actions = []
const thunk = actionCreator(opts.firstarg, opts.secondarg)
thunk(action => actions.push(action), mockGetState)
expect(actions).toHaveLength(1)
expect(actions[0]).toHaveProperty('type')
})
// real interaction with server
if (opts.types.request) {
const properties = opts.properties.request
const propmsg = properties
? `with [${properties.join(', ')}] `
: ''
it(`dispatches ${key}Request ${propmsg}immediately`, () => {
let dispatched
const dispatch = typedmsg => {
if (!dispatched) dispatched = typedmsg
}
let fetcher = action(opts.firstarg, opts.secondarg)
fetcher(dispatch, mockGetState)
it(`dispatches ${key}Request ${propmsg}immediately`, () => {
const actions = []
const thunk = actionCreator(opts.firstarg, opts.secondarg)
thunk(action => actions.push(action), mockGetState)
expect(dispatched).toBeTruthy()
expect(dispatched.type).toBe(opts.types.request)
const firstAction = actions[0]
expect(firstAction).toBeTruthy()
expect(firstAction.type).toBe(opts.types.request)
if (properties) {
expect(dispatched).toHaveProperties(properties)
expect(firstAction).toHaveProperties(properties)
}
data[opts.types.request] = dispatched
})
}
......@@ -122,18 +88,16 @@ const describeAction = actions => (key, opts, cb) => {
: ''
it(`dispatches ${key}Success ${propmsg}on successful response`, async () => {
let fetcher = action(opts.firstarg, opts.secondarg)
const dispatched = await fetcher(
typedmsg => Promise.resolve(typedmsg),
mockGetState
)
expect(dispatched).toBeTruthy()
expect(dispatched.type).toBe(opts.types.success)
const actions = []
const thunk = actionCreator(opts.firstarg, opts.secondarg)
await thunk(action => actions.push(action), mockGetState)
const secondAction = actions[1]
expect(secondAction).toBeTruthy()
expect(secondAction.type).toBe(opts.types.success)
if (properties) {
expect(dispatched).toHaveProperties(properties)
expect(secondAction).toHaveProperties(properties)
}
data[opts.types.success] = dispatched
})
}
......@@ -147,18 +111,16 @@ const describeAction = actions => (key, opts, cb) => {
// make API reject every request
mockApi(false)
let fetcher = action(opts.firstarg, opts.secondarg)
const dispatched = await fetcher(
typedmsg => Promise.resolve(typedmsg),
mockGetState
)
const actions = []
const thunk = actionCreator(opts.firstarg, opts.secondarg)
await thunk(action => actions.push(action), mockGetState)
expect(dispatched).toBeTruthy()
expect(dispatched.type).toBe(opts.types.failure)
const secondAction = actions[1]
expect(secondAction).toBeTruthy()
expect(secondAction.type).toBe(opts.types.failure)
if (properties) {
expect(dispatched).toHaveProperties(properties)
expect(secondAction).toHaveProperties(properties)
}
data[opts.types.failure] = dispatched
})
}
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment