Skip to content

Instantly share code, notes, and snippets.

@marr
Created March 1, 2016 22:18
Show Gist options
  • Save marr/c9a5539e3083375618f4 to your computer and use it in GitHub Desktop.
Save marr/c9a5539e3083375618f4 to your computer and use it in GitHub Desktop.
import { test } from 'tap'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { asyncChangeUser, loadUser, loadCreditCards } from '../actions'
import {
ACCOUNT_ENDPOINT,
ACCOUNT_EDIT_ENDPOINT,
STATUS_ENDPOINT,
CENTRE_ENDPOINT
} from 'utils/api'
import { getHostname, getPath } from 'utils/http'
import { CHANGE_USER, CHANGED_USER, LOAD_USER, LOADED_USER,
RECEIVED_ACCOUNT, RECEIVED_CENTRE, RECEIVED_STATUS } from '../constants'
import nock from 'nock'
test('should get full guest', assert => {
const personId = 'WF-foo'
const testAccountData = {
"created_at": "2016-02-04T19:07:28.485Z",
"email": "[email protected]",
"email_marketing_opt_in": false,
"first_name": "dave",
"last_name": "test",
"newsletter_centre_ids": [],
"newsletter_subscriptions": [],
"person_id": personId,
"phone_number": "",
"primary_centre_id": "chermside",
"registered_for_parking": false,
"sms_marketing_opt_in": false,
"updated_at": "2016-02-05T00:43:42.852Z"
}
const testCentreData = {
"short_name" : "Chermside",
"number_of_levels" : 3,
"social_media" : {
"facebook" : "http://www.facebook.com/westfieldchermside",
"google_plus" : "http://plus.google.com/116705982116614986647/posts",
"twitter" : "http://twitter.com/westfieldau",
"youtube" : "http://www.youtube.com/westfieldfashion",
"pinterest" : "http://pinterest.com/westfieldau",
"instagram" : "http://instagram.com/westfieldchermside"
},
"owner" : "Scentre Group",
"suburb" : "Chermside/Brisbane",
"name" : "Westfield Chermside",
"type" : "centre",
"phone_number" : "(07) 3117 5300",
"map_id" : "308",
"postcode" : "4032",
"email_address" : "[email protected]",
"enabled" : true,
"enabled_at" : "2015-06-15T07:10:17.263Z",
"country" : "AU",
"disabled_at" : null,
"time_zone" : "Australia/Queensland",
"features" : [
"cinema",
"dining",
"retail"
],
"geofences" : {},
"_links" : {
"products" : {
"href" : "https://api.uat.westfield.io/v1/search/products?centre_id=chermside"
},
"deals" : {
"href" : "https://api.uat.westfield.io/v1/deals?centre_id=chermside"
},
"dining_image_medium" : {
"href" : "http://res.cloudinary.com/wlabs/image/upload/gsvvryjoedan6gz29gi3.jpg"
},
"self" : {
"href" : "https://api.uat.westfield.io/v1/centres/chermside"
},
"trading_hours" : {
"href" : "https://api.uat.westfield.io/v1/centres/chermside/trading-hours"
},
"dining_image" : {
"href" : "http://res.cloudinary.com/wlabs/image/upload/ebuztpup7wnwq7sqjbl6.jpg"
},
"events" : {
"href" : "https://api.uat.westfield.io/v1/events?centre_id=chermside"
},
"hero_image" : {
"href" : "http://res.cloudinary.com/wlabs/image/upload/y6buq4xojeangufdaznj.jpg"
},
"dining_image_small" : {
"href" : "http://res.cloudinary.com/wlabs/image/upload/ihrpngyiwe2iluwklhaa.jpg"
},
"stores" : {
"href" : "https://api.uat.westfield.io/v1/stores?centre_id=chermside"
}
},
"updated_at" : "2016-02-15T05:38:18.389Z",
"latitude" : -27.384408,
"street_address" : "Cnr Gympie & Hamilton Rds",
"business_unit_code" : "26721",
"state" : "QLD",
"longitude" : 153.032145,
"centre_id" : "chermside",
"theme" : "aspirational"
}
const accountEndpoint = ACCOUNT_ENDPOINT(personId)
// Mock account response
nock(getHostname(accountEndpoint))
.get(getPath(accountEndpoint))
.reply(200, {
"data": testAccountData,
"errors": {},
"meta": {
"api_version": "1",
"deprecation_information": {}
}
})
const centreEndpoint = CENTRE_ENDPOINT(testAccountData.primary_centre_id)
// Mock status response
nock(getHostname(centreEndpoint))
.get(getPath(centreEndpoint))
.reply(200, {
"data": testCentreData,
"errors": {},
"meta": {
"api_version": "1",
"deprecation_information": {}
}
})
// Mock status response
nock(getHostname(STATUS_ENDPOINT))
.get(getPath(STATUS_ENDPOINT) + `&[email protected]&country=US`)
.reply(200, {
"data": "full",
"errors": {},
"meta": {
"api_version": "1",
"deprecation_information": {}
}
})
// expected action payloads
const receivedAccountPayload = { account: testAccountData, personId }
const receivedStatusPayload = { status: "full", personId }
const receivedCentrePayload = { centre: testCentreData, personId }
const testUserData = {
...receivedAccountPayload,
...receivedCentrePayload,
...receivedStatusPayload
}
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
const expectedActions = [
{ type: LOAD_USER, payload: { personId } },
{ type: RECEIVED_ACCOUNT, payload: receivedAccountPayload },
{ type: RECEIVED_STATUS, payload: receivedStatusPayload },
{ type: RECEIVED_CENTRE, payload: receivedCentrePayload },
{ type: LOADED_USER, payload: { testUserData, personId }}
]
const store = mockStore({}, expectedActions, (err) => {
if (err) throw err
assert.equal(err, null)
assert.ok(1)
assert.end()
})
store.dispatch(loadUser(personId))
})
test('should update customer', assert => {
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
const person_id = 'WF-xyz'
const state = {
person_id,
first_name: 'Doc',
last_name: 'Jones',
phone_number: '415-241-1241',
email: '[email protected]'
}
const expectedState = {
person_id,
email: '[email protected]'
}
// Mock changeUser
nock(getHostname(ACCOUNT_EDIT_ENDPOINT))
.patch(getPath(ACCOUNT_EDIT_ENDPOINT))
.reply(204, {})
const expectedActions = [
{ type: CHANGE_USER },
{ type: CHANGED_USER, payload: expectedState }
]
const store = mockStore(state, expectedActions, (err) => {
assert.notOk(err)
assert.ok(1)
assert.end()
})
store.dispatch(asyncChangeUser({
person_id,
email: '[email protected]'
}))
});
test('after all', assert => {
nock.cleanAll()
assert.end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment