|
| 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 | + __esModule: true, |
| 11 | + default: function Filter() { |
| 12 | + return null; |
| 13 | + }, |
| 14 | +})); |
| 15 | +jest.mock( |
| 16 | + 'context/currentApp', |
| 17 | + () => ({ |
| 18 | + CurrentApp: null, |
| 19 | + }), |
| 20 | + { virtual: true } |
| 21 | +); |
| 22 | + |
| 23 | +const Filter = require('../../components/Filter/Filter.react').default; |
| 24 | +const PushAudienceDialog = |
| 25 | + require('../../components/PushAudienceDialog/PushAudienceDialog.react').default; |
| 26 | +const React = require('react'); |
| 27 | +const { List, Map } = require('immutable'); |
| 28 | + |
| 29 | +const defaultProps = { |
| 30 | + availableDevices: [], |
| 31 | + primaryAction: jest.fn(), |
| 32 | + secondaryAction: jest.fn(), |
| 33 | +}; |
| 34 | + |
| 35 | +function createDialog(schema, audienceInfo) { |
| 36 | + const dialog = new PushAudienceDialog(); |
| 37 | + dialog.props = { |
| 38 | + ...defaultProps, |
| 39 | + audienceInfo, |
| 40 | + schema, |
| 41 | + }; |
| 42 | + dialog.fetchAudienceSize = jest.fn(); |
| 43 | + dialog.setState = (update, callback) => { |
| 44 | + const stateUpdate = typeof update === 'function' ? update(dialog.state, dialog.props) : update; |
| 45 | + dialog.state = { |
| 46 | + ...dialog.state, |
| 47 | + ...stateUpdate, |
| 48 | + }; |
| 49 | + callback?.(); |
| 50 | + }; |
| 51 | + return dialog; |
| 52 | +} |
| 53 | + |
| 54 | +function findElementByType(children, type) { |
| 55 | + let match; |
| 56 | + React.Children.forEach(children, child => { |
| 57 | + if (match || !React.isValidElement(child)) { |
| 58 | + return; |
| 59 | + } |
| 60 | + if (child.type === type) { |
| 61 | + match = child; |
| 62 | + return; |
| 63 | + } |
| 64 | + match = findElementByType(child.props.children, type); |
| 65 | + }); |
| 66 | + return match; |
| 67 | +} |
| 68 | + |
| 69 | +describe('PushAudienceDialog', () => { |
| 70 | + beforeEach(() => { |
| 71 | + jest.clearAllMocks(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('configures the shared filter for the Installation class', () => { |
| 75 | + const schema = { |
| 76 | + deviceType: { type: 'String' }, |
| 77 | + }; |
| 78 | + const dialog = createDialog(schema); |
| 79 | + const filter = findElementByType(dialog.render().props.children, Filter); |
| 80 | + |
| 81 | + expect(filter).toBeDefined(); |
| 82 | + expect(filter.props.className).toBe('_Installation'); |
| 83 | + expect(filter.props.schema).toEqual({ _Installation: schema }); |
| 84 | + expect(filter.props.allClasses).toEqual({ _Installation: schema }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('adds an available audience condition for the Installation class', () => { |
| 88 | + const dialog = createDialog({ |
| 89 | + deviceType: { type: 'String' }, |
| 90 | + }); |
| 91 | + |
| 92 | + dialog.handleAddCondition(); |
| 93 | + |
| 94 | + expect(dialog.state.filters.size).toBe(1); |
| 95 | + expect(dialog.state.filters.getIn([0, 'class'])).toBe('_Installation'); |
| 96 | + expect(dialog.state.filters.getIn([0, 'field'])).toBe('deviceType'); |
| 97 | + expect(dialog.state.filters.getIn([0, 'constraint'])).toBe('exists'); |
| 98 | + expect(dialog.fetchAudienceSize).toHaveBeenCalledTimes(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it('shows an error when no audience condition is available', () => { |
| 102 | + const dialog = createDialog({ |
| 103 | + unsupported: { type: 'File' }, |
| 104 | + }); |
| 105 | + |
| 106 | + dialog.handleAddCondition(); |
| 107 | + |
| 108 | + expect(dialog.state.filters.size).toBe(0); |
| 109 | + expect(dialog.state.errorMessage).toBe('No condition available.'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('normalizes persisted audience filters for the Installation class', () => { |
| 113 | + const filters = new List([ |
| 114 | + new Map({ |
| 115 | + field: 'deviceType', |
| 116 | + constraint: 'exists', |
| 117 | + }), |
| 118 | + ]); |
| 119 | + const dialog = createDialog( |
| 120 | + { deviceType: { type: 'String' } }, |
| 121 | + { |
| 122 | + filters, |
| 123 | + } |
| 124 | + ); |
| 125 | + |
| 126 | + dialog.componentWillMount(); |
| 127 | + |
| 128 | + expect(dialog.state.filters.getIn([0, 'class'])).toBe('_Installation'); |
| 129 | + expect(dialog.fetchAudienceSize).toHaveBeenCalledTimes(1); |
| 130 | + }); |
| 131 | + |
| 132 | + it('clears stale errors when filters change', () => { |
| 133 | + const schema = { |
| 134 | + deviceType: { type: 'String' }, |
| 135 | + }; |
| 136 | + const dialog = createDialog(schema); |
| 137 | + const filters = new List([ |
| 138 | + new Map({ |
| 139 | + class: '_Installation', |
| 140 | + field: 'deviceType', |
| 141 | + constraint: 'exists', |
| 142 | + }), |
| 143 | + ]); |
| 144 | + dialog.state.errorMessage = 'No condition available.'; |
| 145 | + const filter = findElementByType(dialog.render().props.children, Filter); |
| 146 | + |
| 147 | + filter.props.onChange(filters); |
| 148 | + |
| 149 | + expect(dialog.state.filters).toBe(filters); |
| 150 | + expect(dialog.state.errorMessage).toBeUndefined(); |
| 151 | + expect(dialog.fetchAudienceSize).toHaveBeenCalledTimes(1); |
| 152 | + }); |
| 153 | +}); |
0 commit comments