it('should be able to add a chip with whitespace', () => { const spy = sinon.spy(Autosuggest.prototype, 'addChipToPendingQueries'); const wrapper = mount(<Autosuggest options={stubOptions} uiState={createStubUiStateStore()} search={() => {}} serviceStore={createServiceStubStore()} operationStore={createOperationStubStore()}/>); expect(spy.callCount).to.equal(0); const input = wrapper.find('.usb-searchbar__input'); wrapper.instance().inputRef.value = 'serviceName="whitespace test"'; input.prop('onKeyDown')({keyCode: 32, preventDefault: () => {}}); const pendingQuery = wrapper.instance().props.uiState.pendingQuery; expect(spy.callCount).to.equal(1); expect(pendingQuery.length).to.equal(1); Autosuggest.prototype.addChipToPendingQueries.restore(); });
it('should be able to handle range operators', () => { const customOption = {duration: {values: [], isRangeQuery: true}}; const wrapper = mount(<Autosuggest options={customOption} uiState={createStubUiStateStore()} search={() => {}} serviceStore={createServiceStubStore()} operationStore={createOperationStubStore()}/>); wrapper.instance().inputRef.value = 'duration>10000'; wrapper.instance().addChipToPendingQueries(); const pendingQuery = wrapper.instance().props.uiState.pendingQuery; expect(pendingQuery.length).to.equal(1); expect(pendingQuery[0].key).to.equal('duration'); expect(pendingQuery[0].operator).to.equal('>'); });
it('should add new searches to the history cookie', () => { global.window.document.cookie = 'searchhistory=[]'; // reset search history cookie const stubQueryOne = {search: '?query_1.serviceName=root-service&time.preset=1h'}; const stubQueryTwo = {search: '?query_1.serviceName=new-root-service&query_1.error=true&time.preset=4h'}; const wrapper = mount(<MemoryRouter><UniversalSearch.WrappedComponent location={stubQueryOne} history={stubHistory}/></MemoryRouter>); expect(wrapper.find('Autosuggest').first().instance().props.uiState.searchHistory.length).to.equal(0); wrapper.setProps({ children: React.cloneElement(wrapper.props().children, { location: stubQueryTwo }) }); expect(wrapper.find('Autosuggest').first().instance().props.uiState.searchHistory.length).to.equal(1); });
it('should build map of node element references based on `id` property', () => { const nodes = [{x: 10, y: 20, data: {id: 'foo'}}]; const wrapper = mount(<Nodes nodes={nodes} onHover={() => {}} onLeave={() => {}} />); const instance = wrapper.instance(); expect(Object.keys(instance.nodeRefs)[0]).to.equal('foo'); });
it("should add new todos to local storage", () => { const setItemMock = jest.fn(); global.localStorage.__proto__.getItem = jest.fn().mockReturnValue("[]"); global.localStorage.__proto__.setItem = setItemMock; const wrapper = mount(<TodoList />); wrapper.setState({ newTodo: "Novo todo" }); wrapper.instance().handleAddTodo(); expect(setItemMock).toHaveBeenLastCalledWith( "todos", JSON.stringify(["Novo todo"]) ); });
it('should unset ProgressBar.interval after unmounting', () => { sinon.spy(ProgressBar.prototype, 'componentWillUnmount'); const renderedComponent = mount( // eslint-disable-line <ProgressBar percent={0} updateProgress={(noop) => noop} /> ); const inst = renderedComponent.instance(); clock.tick(1000); expect(inst.interval).toBeDefined(); renderedComponent.unmount(); expect(inst.interval).toBeUndefined(); ProgressBar.prototype.componentWillUnmount.restore(); });
it('suggestions should disappear when escape is pressed', () => { const wrapper = mount(<Autosuggest options={stubOptions} uiState={createStubUiStateStore()} search={() => {}} serviceStore={createServiceStubStore()} operationStore={createOperationStubStore()}/>); const input = wrapper.find('.usb-searchbar__input'); input.prop('onFocus')({target: {value: ''}}); input.prop('onKeyDown')({keyCode: 27, preventDefault: () => {}}); expect(wrapper.instance().state.suggestionStrings.length).to.equal(0); });
it('if onchange before invalid then error hidden', () => { const wrapper = mount(<Field id="email" name="email" type="email" />); const $input = wrapper.find('input'); $input.instance().value = 'notanemail'; $input.first().simulate('change', { target: { value: 'notanemail' } }); expect(wrapper).toHaveState('invalid', false); expect(wrapper).toHaveState('errorMessage', ''); // Activate validation and show message $input.instance().dispatchEvent(new window.Event('invalid', { bubbles: true })); expect(wrapper).toHaveState('invalid', true); expect(wrapper).toHaveState('errorMessage', 'This value is not an email.'); });
it('should unset ProgressBar.timeout after getting new props', () => { const renderedComponent = mount( // eslint-disable-line <ProgressBar percent={100} updateProgress={(noop) => noop} /> ); const inst = renderedComponent.instance(); clock.tick(1000); expect(inst.timeout).toBeDefined(); inst.componentWillReceiveProps({ percent: 50 }); expect(inst.timeout).toBeUndefined(); });
it('should be able to properly modify a query with whitespace', () => { const wrapper = mount(<Autosuggest options={stubOptions} uiState={createStubUiStateStore(stubWhitespaceQuery)} search={() => {}} serviceStore={createServiceStubStore()} operationStore={createOperationStubStore()}/>); wrapper.instance().modifyQuery(0); expect(wrapper.instance().props.uiState.pendingQuery[0].value).to.equal('whitespace test'); });
it('pattern', () => { const wrapper = mount( <Field defaultValue="(11)123456789" id="tel" name="tel" pattern="^[\d]*$" type="tel" />, ); wrapper .find('input') .instance() .dispatchEvent(new window.Event('invalid', { bubbles: true })); expect(wrapper).toHaveState('invalid', true); expect(wrapper).toHaveState('errorMessage', 'This value is invalid.'); });
it('should be able to add a chip by pressing space bar', () => { const spy = sinon.spy(Autosuggest.prototype, 'addChipToPendingQueries'); const wrapper = mount(<Autosuggest options={stubOptions} uiState={createStubUiStateStore()} search={() => {}} serviceStore={createServiceStubStore()} operationStore={createOperationStubStore()}/>); expect(spy.callCount).to.equal(0); const input = wrapper.find('.usb-searchbar__input'); wrapper.instance().inputRef.value = 'serviceName=test'; input.prop('onKeyDown')({keyCode: 32, preventDefault: () => {}}); const pendingQuery = wrapper.instance().props.uiState.pendingQuery; expect(spy.callCount).to.equal(1); expect(pendingQuery.length).to.equal(1); Autosuggest.prototype.addChipToPendingQueries.restore(); });
it('should unset ProgressBar.interval after unmounting', () => { sinon.spy(ProgressBar.prototype, 'componentWillUnmount'); const renderedComponent = mount( // eslint-disable-line <ProgressBar percent={0} updateProgress={(noop) => noop} /> ); const inst = renderedComponent.instance(); clock.tick(1000); expect(inst.interval).toBeDefined(); renderedComponent.unmount(); expect(inst.interval).toBeUndefined(); ProgressBar.prototype.componentWillUnmount.restore(); });