Skip to content

Commit

Permalink
test(redux style action): test on redux style action class and objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jhe committed Jul 8, 2016
1 parent 7f23371 commit 295617f
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/redux_action_match.spec.coffee
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
61 changes: 61 additions & 0 deletions test/redux_action_match.spec.es5.js
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);
});
});
61 changes: 61 additions & 0 deletions test/redux_action_match.spec.es6.js
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);
});
});

0 comments on commit 295617f

Please sign in to comment.