Skip to content

Commit 75d0b4d

Browse files
Merge pull request #203 from testshallpass/bugfix-close-interval-zero-auto-close
fix closeInterval set to 0 disable auto close
2 parents 1561809 + ed853a9 commit 75d0b4d

File tree

5 files changed

+97
-34
lines changed

5 files changed

+97
-34
lines changed

DropdownAlert.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ export default class DropdownAlert extends Component {
258258
if (!replaceEnabled && isOpen) {
259259
this.alertData = data;
260260
this.setState({ isOpen: true });
261-
this.closeAutomatic(duration);
261+
if (duration > 0) {
262+
this.closeAutomatic(duration);
263+
}
262264
return;
263265
}
264266
if (isOpen) {
@@ -274,7 +276,9 @@ export default class DropdownAlert extends Component {
274276
this.setState({ isOpen: true });
275277
this.animate(1, 450, () => {
276278
this.animationLock = false;
277-
this.closeAutomatic(duration);
279+
if (duration > 0) {
280+
this.closeAutomatic(duration);
281+
}
278282
});
279283
};
280284
closeAction = (action = ACTION.programmatic, onDone = () => {}) => {

__tests__/CancelButton-test.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ import CancelButton from '../CancelButton';
44
import { shallow } from 'enzyme';
55
import toJson from 'enzyme-to-json';
66

7-
test('CancelButton renders', () => {
8-
const wrapper = shallow(<CancelButton />);
9-
const tree = toJson(wrapper);
10-
expect(tree).toMatchSnapshot();
11-
});
7+
describe('CancelButton', () => {
8+
test('renders', () => {
9+
const wrapper = shallow(<CancelButton />);
10+
const tree = toJson(wrapper);
11+
expect(tree).toMatchSnapshot();
12+
});
13+
test('test onPress to be defined', () => {
14+
const onCancel = () => {
15+
console.log('Cancelled');
16+
};
17+
const wrapper = shallow(<CancelButton onPress={onCancel} />);
18+
expect(wrapper.prop('onPress')).toEqual(onCancel);
19+
expect(wrapper.props().onPress).toBeDefined();
20+
});
21+
});

__tests__/Constants-test.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import { getDefaultStatusBarStyle, getDefaultStatusBarBackgroundColor } from '../constants';
22

3-
test('StatusBarDefaultBarStyle to be default', () => {
4-
const barStyle = getDefaultStatusBarStyle();
5-
expect(barStyle).toBe('default');
6-
});
7-
test('StatusBarDefaultBackgroundColor to be black', () => {
8-
const backgroundColor = getDefaultStatusBarBackgroundColor();
9-
expect(backgroundColor).toBe('black');
10-
});
11-
test('StatusBarDefaultBarStyle to be StatusBar._defaultProps.barStyle.value', () => {
12-
const { StatusBar } = require('react-native');
13-
const barStyle = getDefaultStatusBarStyle();
14-
expect(barStyle).toBe(StatusBar._defaultProps.barStyle.value);
15-
});
16-
test('StatusBarDefaultBackgroundColor to be StatusBar._defaultProps.backgroundColor.value', () => {
17-
const { StatusBar } = require('react-native');
18-
const backgroundColor = getDefaultStatusBarBackgroundColor();
19-
expect(backgroundColor).toBe(StatusBar._defaultProps.backgroundColor.value);
3+
describe('Constants', () => {
4+
describe('Mock StatusBar with no _defaultProps property', () => {
5+
beforeEach(() => {
6+
jest.mock('StatusBar', () => {
7+
return {};
8+
});
9+
});
10+
test('StatusBarDefaultBarStyle to be default', () => {
11+
const barStyle = getDefaultStatusBarStyle();
12+
expect(barStyle).toBe('default');
13+
});
14+
test('StatusBarDefaultBackgroundColor to be black', () => {
15+
const backgroundColor = getDefaultStatusBarBackgroundColor();
16+
expect(backgroundColor).toBe('black');
17+
});
18+
});
19+
describe('StatusBar with _defaultProps property', () => {
20+
const { StatusBar } = require('react-native');
21+
test('StatusBarDefaultBarStyle to be StatusBar._defaultProps.barStyle.value', () => {
22+
const barStyle = getDefaultStatusBarStyle();
23+
expect(barStyle).toBe(StatusBar._defaultProps.barStyle.value);
24+
});
25+
test('StatusBarDefaultBackgroundColor to be StatusBar._defaultProps.backgroundColor.value', () => {
26+
const backgroundColor = getDefaultStatusBarBackgroundColor();
27+
expect(backgroundColor).toBe(StatusBar._defaultProps.backgroundColor.value);
28+
});
29+
});
2030
});

__tests__/DropdownAlert-test.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe('DropdownAlert component', () => {
173173
<DropdownAlert imageSrc={imageSrc} showCancel={true} renderTitle={() => {}} renderMessage={() => {}} renderCancel={() => {}} renderImage={() => {}} />
174174
);
175175
wrapper.instance().isOpen = false;
176-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
176+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
177177
wrapper.update();
178178
const type = TYPE.error;
179179
const title = 'Duis duis nostrud excepteur ipsum.';
@@ -189,7 +189,7 @@ describe('DropdownAlert component', () => {
189189
test('expect type custom to be open state and have alert data', () => {
190190
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
191191
wrapper.instance().isOpen = false;
192-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
192+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
193193
wrapper.update();
194194
const type = TYPE.custom;
195195
const title = 'Lorem fugiat reprehenderit non aute elit Lorem quis sit irure non.';
@@ -205,7 +205,7 @@ describe('DropdownAlert component', () => {
205205
test('expect type info to be open state with replaceEnabled prop as false', () => {
206206
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} replaceEnabled={false} />);
207207
wrapper.instance().isOpen = false;
208-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
208+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
209209
wrapper.update();
210210
const type = TYPE.info;
211211
const title = 'Laborum reprehenderit aute sit sunt labore velit consectetur cillum id dolore tempor mollit commodo.';
@@ -222,7 +222,7 @@ describe('DropdownAlert component', () => {
222222
const closeInterval = 4000;
223223
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} closeInterval={closeInterval} />);
224224
wrapper.instance().isOpen = false;
225-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
225+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
226226
wrapper.update();
227227
const type = TYPE.info;
228228
const title = 'Laborum reprehenderit aute sit sunt labore velit consectetur cillum id dolore tempor mollit commodo.';
@@ -236,10 +236,28 @@ describe('DropdownAlert component', () => {
236236
expect(wrapper.instance().state.topValue).toBe(0);
237237
expect(wrapper.instance().closeTimeoutID).toBeDefined();
238238
});
239+
test('expect type info to be open state with closeInterval prop as zero and replaceEnabled false', () => {
240+
const closeInterval = 0;
241+
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} closeInterval={closeInterval} replaceEnabled={false} />);
242+
wrapper.instance().setState({ isOpen: true });
243+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
244+
wrapper.update();
245+
const type = TYPE.info;
246+
const title = 'Laborum reprehenderit aute sit sunt labore velit consectetur cillum id dolore tempor mollit commodo.';
247+
const message = 'Aliquip excepteur dolore ipsum exercitation cupidatat incididunt in.';
248+
wrapper.instance().alertWithType(type, title, message, {});
249+
expect(wrapper.instance().alertData.type).toBe(type);
250+
expect(wrapper.instance().alertData.title).toBe(title);
251+
expect(wrapper.instance().alertData.message).toBe(message);
252+
expect(wrapper.instance().alertData.interval).toBe(closeInterval);
253+
expect(wrapper.instance().state.isOpen).toBeTruthy();
254+
expect(wrapper.instance().state.topValue).toBe(0);
255+
expect(wrapper.instance().closeTimeoutID).toBeDefined();
256+
});
239257
test('expect type success to be open state and have alert data with animationLock as true', () => {
240258
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
241259
wrapper.instance().isOpen = false;
242-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
260+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
243261
wrapper.instance().animationLock = true;
244262
wrapper.update();
245263
const type = TYPE.success;
@@ -268,7 +286,7 @@ describe('DropdownAlert component', () => {
268286
test('expect type unknown to be open state and have alert data', () => {
269287
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
270288
wrapper.instance().isOpen = false;
271-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
289+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
272290
wrapper.update();
273291
const type = 'unknown';
274292
const title = 'Excepteur dolore aute culpa occaecat reprehenderit veniam sint tempor exercitation cillum aliquip id reprehenderit.';
@@ -284,7 +302,7 @@ describe('DropdownAlert component', () => {
284302
test('expect unknown type to be open state and have alert data and close automatic', () => {
285303
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} replaceEnabled={false} />);
286304
wrapper.instance().setState({ isOpen: true });
287-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
305+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
288306
wrapper.update();
289307
const type = 'unknown';
290308
const title = 'Excepteur dolore aute culpa occaecat reprehenderit veniam sint tempor exercitation cillum aliquip id reprehenderit.';
@@ -300,7 +318,7 @@ describe('DropdownAlert component', () => {
300318
test('expect unknown type to be open state and have alert data and close action', () => {
301319
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
302320
wrapper.instance().setState({ isOpen: true });
303-
wrapper.instance().closeTimeoutID = setTimeout(function() {});
321+
wrapper.instance().closeTimeoutID = setTimeout(() => {});
304322
wrapper.update();
305323
const type = 'unknown';
306324
const title = 'Excepteur dolore aute culpa occaecat reprehenderit veniam sint tempor exercitation cillum aliquip id reprehenderit.';
@@ -314,7 +332,13 @@ describe('DropdownAlert component', () => {
314332
expect(wrapper.instance().closeTimeoutID).toBeDefined();
315333
});
316334
});
317-
describe('open', () => {});
335+
describe('open', () => {
336+
test('expect open to be okay with no data', () => {
337+
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
338+
wrapper.instance().open();
339+
expect(wrapper.instance().state.isOpen).toBeTruthy();
340+
});
341+
});
318342
describe('closeAction', () => {
319343
test('expect close with programmatic action to set isOpen to be false', () => {
320344
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
@@ -332,6 +356,11 @@ describe('DropdownAlert component', () => {
332356
expect(wrapper.instance().state.topValue).toBe(0);
333357
});
334358
});
359+
test('expect close without action to be okay', () => {
360+
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
361+
wrapper.setState({ isOpen: true });
362+
wrapper.instance().closeAction();
363+
});
335364
});
336365
describe('closeAutomatic', () => {
337366
test('expect isOpen to be false', () => {
@@ -354,6 +383,10 @@ describe('DropdownAlert component', () => {
354383
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
355384
wrapper.instance().updateStatusBar(false, true);
356385
});
386+
test('expect without parameters to be okay', () => {
387+
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} />);
388+
wrapper.instance().updateStatusBar();
389+
});
357390
});
358391
describe('clearCloseTimeoutID', () => {});
359392
describe('animate', () => {
@@ -363,6 +396,12 @@ describe('DropdownAlert component', () => {
363396
const lock = wrapper.instance().animationLock;
364397
expect(lock).toBeTruthy();
365398
});
399+
test('expect animation lock to be false', () => {
400+
const wrapper = shallow(<DropdownAlert imageSrc={imageSrc} useAnimationLock={false} />);
401+
wrapper.instance().animate(0)
402+
const lock = wrapper.instance().animationLock;
403+
expect(lock).toBeFalsy();
404+
});
366405
});
367406
describe('getStartDelta', () => {
368407
test('expect to return start value', () => {

docs/PROPS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
| Name | Type | Description | Default |
3535
| ---- | :---: | --- | --- |
36-
| ```closeInterval``` | Number | automatic close duration in milliseconds | 4000
36+
| ```closeInterval``` | Number | automatic close duration in milliseconds (set to less than and equal to 0 disables the automatic close) | 4000
3737
| ```startDelta``` | Number | where the alert container starts to animate from (min: negative height) | -100
3838
| ```endDelta``` | Number | where the alert container ends at | 0
3939
| ```useAnimationLock``` | Bool | used to avoid animation collision (i.e. alert is invoked while one is animating open or close) | true

0 commit comments

Comments
 (0)