-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(redux style action): test on redux style action class and objects
- Loading branch information
jhe
committed
Jul 8, 2016
1 parent
7f23371
commit 295617f
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
chai = require 'chai' | ||
matchJS = require '../lib/match-js' | ||
match = matchJS.match | ||
otherwise = matchJS.otherwise | ||
|
||
chai.expect() | ||
expect = chai.expect | ||
|
||
describe 'Matching on redux-like Action class:', -> | ||
class Action | ||
constructor:(type) -> | ||
@type = type | ||
@payload = undefined | ||
|
||
unapply:(action) -> | ||
if(action.type == @type) | ||
return { payload: action.payload } | ||
|
||
action:(payload)-> | ||
return { | ||
type: this.type | ||
payload: payload | ||
} | ||
|
||
ChangeUsername = new Action('ChangeUsername') | ||
ChangeAddress = new Action('ChangeAddress') | ||
StrangeAction = new Action('UJRBJLWYTU') | ||
|
||
it 'ChangeUsername should match ChangeUsername', -> | ||
action = ChangeUsername.action('myusername') | ||
m = match(action)( | ||
[ChangeUsername, ({payload}) -> 'name: '+payload], | ||
[ChangeAddress, ({payload}) -> 'postcode: '+payload.postcode], | ||
[otherwise, (_) -> 'unknown action: '+action] | ||
) | ||
expect(m).to.be.equal('name: myusername') | ||
|
||
it 'ChangeAddress should match ChangeAddress', -> | ||
action = ChangeAddress.action({ | ||
name: 'HOME ROAD', | ||
postcode: '1234', | ||
}) | ||
m = match(action)( | ||
[ChangeUsername, ({payload}) -> 'name: '+payload], | ||
[ChangeAddress, ({payload}) -> 'postcode: '+payload.postcode], | ||
[otherwise, (_) -> 'unknown action: '+action] | ||
) | ||
expect(m).to.be.equal('postcode: 1234') | ||
|
||
it 'StrangeAction should match otherwise', -> | ||
action = StrangeAction.action('FSCRKCAWCN') | ||
m = match(action)( | ||
[ChangeUsername, ({payload}) -> 'name: '+payload], | ||
[ChangeAddress, ({payload}) -> 'postcode: '+payload.postcode], | ||
[otherwise, (_) -> 'unknown action: '+action] | ||
) | ||
expect(m).to.be.equal('unknown action: '+action) | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var chai = require('chai'); | ||
var matchJS = require('../lib/match-js'); | ||
var match = matchJS.match; | ||
var otherwise = matchJS.otherwise; | ||
|
||
chai.expect(); | ||
var expect = chai.expect; | ||
|
||
describe('Matching on redux-like Action class:', function() { | ||
function Action(type) { | ||
this.type = type; | ||
this.payload = undefined; | ||
} | ||
Action.prototype.unapply = function unapply(action) { | ||
if(action.type === this.type) { | ||
return { payload: action.payload }; | ||
} | ||
}; | ||
Action.prototype.action = function action(payload) { | ||
return { | ||
type: this.type, | ||
payload: payload, | ||
}; | ||
}; | ||
|
||
|
||
var ChangeUsername = new Action('ChangeUsername'); | ||
var ChangeAddress = new Action('ChangeAddress'); | ||
var StrangeAction = new Action('UJRBJLWYTU'); | ||
|
||
it('ChangeUsername should match ChangeUsername', function() { | ||
var action = ChangeUsername.action('myusername'); | ||
var m = match(action)( | ||
[ChangeUsername, function(payload) {return 'name: '+payload.payload;}], | ||
[ChangeAddress, function(payload) {return 'postcode: '+payload.payload.postcode;}], | ||
[otherwise, function() {return 'unknown action: '+action;}] | ||
); | ||
expect(m).to.be.equal('name: myusername'); | ||
}); | ||
it('ChangeAddress should match ChangeAddress', function() { | ||
var action = ChangeAddress.action({ | ||
name: 'HOME ROAD', | ||
postcode: '1234', | ||
}); | ||
var m = match(action)( | ||
[ChangeUsername, function(payload) {return 'name: '+payload.payload;}], | ||
[ChangeAddress, function(payload) {return 'postcode: '+payload.payload.postcode;}], | ||
[otherwise, function() {return 'unknown action: '+action;}] | ||
); | ||
expect(m).to.be.equal('postcode: 1234'); | ||
}); | ||
it('StrangeAction should match otherwise', function() { | ||
var action = StrangeAction.action('FSCRKCAWCN'); | ||
var m = match(action)( | ||
[ChangeUsername, function(payload) {return 'name: '+payload.payload;}], | ||
[ChangeAddress, function(payload) {return 'postcode: '+payload.payload.postcode;}], | ||
[otherwise, function() {return 'unknown action: '+action;}] | ||
); | ||
expect(m).to.be.equal('unknown action: '+action); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import chai from 'chai'; | ||
import { match, otherwise }from '../lib/match-js'; | ||
|
||
chai.expect(); | ||
const expect = chai.expect; | ||
|
||
describe('Matching on redux-like Action class:', () => { | ||
class Action { | ||
constructor(type) { | ||
this.type = type; | ||
this.payload = undefined; | ||
} | ||
unapply(action) { | ||
if(action.type === this.type) { | ||
return { payload: action.payload }; | ||
} | ||
} | ||
|
||
action(payload){ | ||
return { | ||
type: this.type, | ||
payload: payload, | ||
} | ||
} | ||
} | ||
|
||
const ChangeUsername = new Action('ChangeUsername'); | ||
const ChangeAddress = new Action('ChangeAddress'); | ||
const StrangeAction = new Action('UJRBJLWYTU'); | ||
|
||
it('ChangeUsername should match ChangeUsername', () => { | ||
const action = ChangeUsername.action('myusername'); | ||
const m = match(action)( | ||
[ChangeUsername, ({payload}) => 'name: '+payload], | ||
[ChangeAddress, ({payload}) => 'postcode: '+payload.postcode], | ||
[otherwise, (_) => 'unknown action: '+action] | ||
); | ||
expect(m).to.be.equal('name: myusername'); | ||
}); | ||
it('ChangeAddress should match ChangeAddress', () => { | ||
const action = ChangeAddress.action({ | ||
name: 'HOME ROAD', | ||
postcode: '1234', | ||
}); | ||
const m = match(action)( | ||
[ChangeUsername, ({payload}) => 'name: '+payload], | ||
[ChangeAddress, ({payload}) => 'postcode: '+payload.postcode], | ||
[otherwise, (_) => 'unknown action: '+action] | ||
); | ||
expect(m).to.be.equal('postcode: 1234'); | ||
}); | ||
it('StrangeAction should match otherwise', () => { | ||
const action = StrangeAction.action('FSCRKCAWCN'); | ||
const m = match(action)( | ||
[ChangeUsername, ({payload}) => 'name: '+payload], | ||
[ChangeAddress, ({payload}) => 'postcode: '+payload.postcode], | ||
[otherwise, (_) => 'unknown action: '+action] | ||
); | ||
expect(m).to.be.equal('unknown action: '+action); | ||
}); | ||
}); |