-
Notifications
You must be signed in to change notification settings - Fork 79
/
BulkActions-test.js
150 lines (129 loc) · 4.41 KB
/
BulkActions-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* global jest, jasmine, describe, it, expect, beforeEach */
jest.mock('jquery', () => {
const jqueryMock = {
find: () => jqueryMock,
change: () => jqueryMock,
val: () => jqueryMock,
trigger: () => null,
chosen: () => null,
on: () => null,
off: () => null,
};
return () => jqueryMock;
});
import React from 'react';
import ReactTestUtils from 'react-dom/test-utils';
import { Component as BulkActions } from '../BulkActions';
describe('BulkActions', () => {
describe('canApply()', () => {
let props = null;
beforeEach(() => {
props = {
actions: [
{
value: 'action-with-apply',
label: '',
canApply: (items) => items.filter(item => item.applies).length,
callback: () => true,
},
{
value: 'action-without-apply',
label: '',
callback: () => true,
},
],
};
});
it('shows an action button when canApply returns true', () => {
const propsWithItems = Object.assign({}, props, { items: [{ applies: true }] });
const bulkActions = ReactTestUtils.renderIntoDocument(
<BulkActions {...propsWithItems} />
);
const matchedBulkAction = ReactTestUtils.scryRenderedDOMComponentsWithClass(bulkActions, 'bulk-actions__action')
.find(el => el.value === 'action-with-apply');
expect(matchedBulkAction).toBeTruthy();
});
it('does not show an action button when canApply returns false', () => {
const propsWithItems = Object.assign({}, props, { items: [{ applies: false }] });
const bulkActions = ReactTestUtils.renderIntoDocument(
<BulkActions {...propsWithItems} />
);
const matchedBulkAction = ReactTestUtils.scryRenderedDOMComponentsWithClass(bulkActions, 'bulk-actions__action')
.find(el => el.value === 'action-with-apply');
expect(matchedBulkAction).toBeFalsy();
});
});
describe('getOptionByValue()', () => {
let bulkActions = null;
let props = null;
beforeEach(() => {
props = {
actions: [
{
value: 'my-first-action',
label: 'My First Action',
callback: () => true,
},
],
items: [],
};
bulkActions = ReactTestUtils.renderIntoDocument(
<BulkActions {...props} />
);
});
it('should return the option which matches the given value', () => {
expect(bulkActions.getOptionByValue('my-first-action').value).not.toBeFalsy();
});
it('should return null if no option matches the given value', () => {
expect(bulkActions.getOptionByValue('unknown-action')).toBeFalsy();
});
});
describe('handleChangeValue()', () => {
let bulkActions = null;
let event = null;
let props = null;
beforeEach(() => {
props = {
actions: [],
items: [],
};
bulkActions = ReactTestUtils.renderIntoDocument(
<BulkActions {...props} />
);
event = {
target: {
value: null,
},
};
bulkActions.getOptionByValue = jest.fn();
bulkActions.applyAction = jest.fn();
});
it('should return undefined if no valid option is selected', () => {
bulkActions.getOptionByValue.mockReturnValueOnce(null);
expect(bulkActions.handleChangeValue(event)).toBeFalsy();
});
it('should use callback if no confirm callback is configured', () => {
const callbackMockFn = jest.fn();
bulkActions.getOptionByValue.mockReturnValueOnce({ confirm: null, callback: callbackMockFn });
return bulkActions.handleChangeValue(event).then(() => {
expect(callbackMockFn).toBeCalled();
});
});
it('should use callback if confirm is configured and resolved', () => {
const callbackMockFn = jest.fn();
bulkActions.getOptionByValue
.mockReturnValueOnce({ confirm: Promise.resolve(), callback: callbackMockFn });
return bulkActions.handleChangeValue(event).then(() => {
expect(callbackMockFn).toBeCalled();
});
});
it('should not use callback if confirm is configured and rejected', () => {
const callbackMockFn = jest.fn();
bulkActions.getOptionByValue
.mockReturnValueOnce({ confirm: Promise.reject(), callback: callbackMockFn });
return bulkActions.handleChangeValue(event).then(() => {
expect(callbackMockFn).toBeCalled();
});
});
});
});