Completed
Push — master ( 1719ac...509a65 )
by Jeroen De
10s
created

calls validatorꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
1
'use strict';
2
3
var test = require( 'tape' ),
4
	sinon = require( 'sinon' ),
5
	Actions = require( '../../lib/actions' ),
6
	createAmountValidationDispatcher = require( '../../lib/validation_dispatchers/amount' );
7
8
test( 'AmountValidationDispatcher calls validator', function ( t ) {
9
	var successResult = { status: 'OK' },
10
		initialData = {},
11
		testData = { amount: '2.00', ignoredData: 'this won\'t be validated' },
12
		validator = { validate: sinon.stub().returns( successResult ) },
13
		testStore = { dispatch: sinon.stub() },
14
		dispatcher = createAmountValidationDispatcher(
15
			validator,
16
			initialData
17
		);
18
19
	dispatcher.dispatchIfChanged( testData, testStore );
20
21
	t.ok( validator.validate.calledOnce, 'validation function is called once' );
22
	t.ok( validator.validate.calledWith( { amount: '2.00' }  ), 'validation function is called with selected fields' );
23
	t.end();
24
} );
25
26
test( 'AmountValidationDispatcher dispatches result as action', function ( t ) {
27
	var successResult = { status: 'OK' },
28
		initialData = {},
29
		testData = { amount: '2.00', ignoredData: 'this won\'t be validated' },
30
		validator = { validate: sinon.stub().returns( successResult ) },
31
		testStore = { dispatch: sinon.spy() },
32
		dispatcher = createAmountValidationDispatcher(
33
			validator,
34
			initialData
35
		);
36
37
	dispatcher.dispatchIfChanged( testData, testStore );
38
39
	t.ok( testStore.dispatch.calledWith( Actions.newFinishPaymentDataValidationAction( successResult ) ) );
40
	t.end();
41
} );
42
43
test( 'AmountValidationDispatcher calls validator and dispatches action every time the data changes', function ( t ) {
44
	var successResult = { status: 'OK' },
45
		initialData = {},
46
		validator = { validate: sinon.stub().returns( successResult ) },
47
		testStore = { dispatch: sinon.spy() },
48
		dispatcher = createAmountValidationDispatcher(
49
			validator,
50
			initialData
51
		);
52
53
	dispatcher.dispatchIfChanged( { amount: '2.00' }, testStore );
54
	dispatcher.dispatchIfChanged( { amount: '99.00' }, testStore );
55
56
	t.ok( validator.validate.calledTwice, 'validation function is called for every change' );
57
	t.ok( testStore.dispatch.calledTwice, 'action is dispatched for every change' );
58
59
	t.end();
60
} );
61
62
test( 'AmountValidationDispatcher does nothing if data does not change', function ( t ) {
63
	var initialData = { amount: '2.00' },
64
		validator = { validate: sinon.spy() },
65
		testStore = { dispatch: sinon.spy() },
66
		dispatcher = createAmountValidationDispatcher(
67
			validator,
68
			initialData
69
		);
70
71
	dispatcher.dispatchIfChanged( initialData, testStore );
72
73
	t.notOk( validator.validate.called, 'validation function is never called' );
74
	t.notOk( testStore.dispatch.called, 'no action is dispatched' );
75
76
	t.end();
77
} );
78
79
test( 'ValidationDispatcher does nothing if ignored data changes', function ( t ) {
80
	var testData = { ignoredData: 'this won\'t be validated' },
81
		initialData = {},
82
		validator = { validate: sinon.spy() },
83
		testStore = { dispatch: sinon.spy() },
84
		dispatcher = createAmountValidationDispatcher(
85
			validator,
86
			initialData
87
		);
88
89
	dispatcher.dispatchIfChanged( testData, testStore );
90
91
	t.notOk( validator.validate.called, 'validation function is never called' );
92
	t.notOk( testStore.dispatch.called, 'no action is dispatched' );
93
94
	t.end();
95
} );
96