|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present, Parse, LLC |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the LICENSE file in |
| 6 | + * the root directory of this source tree. |
| 7 | + */ |
| 8 | +jest.dontMock('../../components/PushAudienceDialog/PushAudienceDialog.react'); |
| 9 | +jest.mock('../../components/Filter/Filter.react'); |
| 10 | +jest.mock('../../components/MultiSelect/MultiSelect.react'); |
| 11 | +jest.mock('../../components/Popover/Popover.react', () => 'div'); |
| 12 | +jest.mock('context/currentApp', () => require('../../context/currentApp'), { virtual: true }); |
| 13 | + |
| 14 | +const Filter = require('../../components/Filter/Filter.react').default; |
| 15 | +const PushAudienceDialog = |
| 16 | + require('../../components/PushAudienceDialog/PushAudienceDialog.react').default; |
| 17 | +const React = require('react'); |
| 18 | +const { act } = React; |
| 19 | +const { List, Map } = require('immutable'); |
| 20 | +const { renderComponent } = require('./renderWithAct'); |
| 21 | + |
| 22 | +const defaultProps = { |
| 23 | + availableDevices: [], |
| 24 | + primaryAction: jest.fn(), |
| 25 | + secondaryAction: jest.fn(), |
| 26 | +}; |
| 27 | + |
| 28 | +function renderDialog(schema, audienceInfo) { |
| 29 | + return renderComponent( |
| 30 | + <PushAudienceDialog {...defaultProps} audienceInfo={audienceInfo} schema={schema} /> |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +describe('PushAudienceDialog', () => { |
| 35 | + beforeEach(() => { |
| 36 | + jest.spyOn(PushAudienceDialog.prototype, 'fetchAudienceSize').mockImplementation(() => {}); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + jest.restoreAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('configures the shared filter for the Installation class', () => { |
| 44 | + const schema = { |
| 45 | + deviceType: { type: 'String' }, |
| 46 | + }; |
| 47 | + const component = renderDialog(schema); |
| 48 | + const filter = component.root.findByType(Filter); |
| 49 | + |
| 50 | + expect(filter.props.className).toBe('_Installation'); |
| 51 | + expect(filter.props.schema).toEqual({ _Installation: schema }); |
| 52 | + expect(filter.props.allClasses).toEqual({ _Installation: schema }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('adds an available audience condition for the Installation class', () => { |
| 56 | + const component = renderDialog({ |
| 57 | + deviceType: { type: 'String' }, |
| 58 | + }); |
| 59 | + const dialog = component.getInstance(); |
| 60 | + |
| 61 | + act(() => { |
| 62 | + dialog.setState({ errorMessage: 'No condition available.' }); |
| 63 | + }); |
| 64 | + act(() => { |
| 65 | + dialog.handleAddCondition(); |
| 66 | + }); |
| 67 | + |
| 68 | + expect(dialog.state.filters.size).toBe(1); |
| 69 | + expect(dialog.state.filters.getIn([0, 'class'])).toBe('_Installation'); |
| 70 | + expect(dialog.state.filters.getIn([0, 'field'])).toBe('deviceType'); |
| 71 | + expect(dialog.state.filters.getIn([0, 'constraint'])).toBe('exists'); |
| 72 | + expect(dialog.state.errorMessage).toBeUndefined(); |
| 73 | + expect(dialog.fetchAudienceSize).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it('shows an error when no audience condition is available', () => { |
| 77 | + const component = renderDialog({ |
| 78 | + unsupported: { type: 'File' }, |
| 79 | + }); |
| 80 | + const dialog = component.getInstance(); |
| 81 | + |
| 82 | + act(() => { |
| 83 | + dialog.handleAddCondition(); |
| 84 | + }); |
| 85 | + |
| 86 | + expect(dialog.state.filters.size).toBe(0); |
| 87 | + expect(dialog.state.errorMessage).toBe('No condition available.'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('normalizes persisted audience filters for the Installation class', () => { |
| 91 | + const filters = new List([ |
| 92 | + new Map({ |
| 93 | + field: 'deviceType', |
| 94 | + constraint: 'exists', |
| 95 | + }), |
| 96 | + ]); |
| 97 | + const component = renderDialog( |
| 98 | + { deviceType: { type: 'String' } }, |
| 99 | + { |
| 100 | + filters, |
| 101 | + } |
| 102 | + ); |
| 103 | + const dialog = component.getInstance(); |
| 104 | + |
| 105 | + expect(dialog.state.filters.getIn([0, 'class'])).toBe('_Installation'); |
| 106 | + expect(dialog.fetchAudienceSize).toHaveBeenCalledTimes(1); |
| 107 | + }); |
| 108 | + |
| 109 | + it('clears stale errors when filters change', () => { |
| 110 | + const schema = { |
| 111 | + deviceType: { type: 'String' }, |
| 112 | + }; |
| 113 | + const component = renderDialog(schema); |
| 114 | + const dialog = component.getInstance(); |
| 115 | + const filters = new List([ |
| 116 | + new Map({ |
| 117 | + class: '_Installation', |
| 118 | + field: 'deviceType', |
| 119 | + constraint: 'exists', |
| 120 | + }), |
| 121 | + ]); |
| 122 | + act(() => { |
| 123 | + dialog.setState({ errorMessage: 'No condition available.' }); |
| 124 | + }); |
| 125 | + const filter = component.root.findByType(Filter); |
| 126 | + |
| 127 | + act(() => { |
| 128 | + filter.props.onChange(filters); |
| 129 | + }); |
| 130 | + |
| 131 | + expect(dialog.state.filters).toBe(filters); |
| 132 | + expect(dialog.state.errorMessage).toBeUndefined(); |
| 133 | + expect(dialog.fetchAudienceSize).toHaveBeenCalledTimes(1); |
| 134 | + }); |
| 135 | + |
| 136 | + it('clears stale errors when filters are searched', () => { |
| 137 | + const component = renderDialog({ |
| 138 | + deviceType: { type: 'String' }, |
| 139 | + }); |
| 140 | + const dialog = component.getInstance(); |
| 141 | + act(() => { |
| 142 | + dialog.setState({ errorMessage: 'No condition available.' }); |
| 143 | + }); |
| 144 | + const filter = component.root.findByType(Filter); |
| 145 | + |
| 146 | + act(() => { |
| 147 | + filter.props.onSearch(); |
| 148 | + }); |
| 149 | + |
| 150 | + expect(dialog.state.errorMessage).toBeUndefined(); |
| 151 | + expect(dialog.fetchAudienceSize).toHaveBeenCalledTimes(1); |
| 152 | + }); |
| 153 | +}); |
0 commit comments