|
| 1 | +import { createSearchClient } from '@instantsearch/mocks'; |
| 2 | +import algoliasearchHelper from 'algoliasearch-helper'; |
| 3 | + |
| 4 | +import { |
| 5 | + createInitOptions, |
| 6 | + createRenderOptions, |
| 7 | +} from '../../../../test/createWidget'; |
| 8 | +import connectChat from '../connectChat'; |
| 9 | + |
| 10 | +describe('connectChat', () => { |
| 11 | + it('throws without render function', () => { |
| 12 | + expect(() => { |
| 13 | + // @ts-expect-error |
| 14 | + connectChat()({}); |
| 15 | + }).toThrowErrorMatchingInlineSnapshot(` |
| 16 | + "The render function is not valid (received type Undefined). |
| 17 | +
|
| 18 | + See documentation: https://www.algolia.com/doc/api-reference/widgets/chat/js/#connector" |
| 19 | + `); |
| 20 | + }); |
| 21 | + |
| 22 | + it('is a widget', () => { |
| 23 | + const render = jest.fn(); |
| 24 | + const unmount = jest.fn(); |
| 25 | + |
| 26 | + const customChat = connectChat(render, unmount); |
| 27 | + const widget = customChat({}); |
| 28 | + |
| 29 | + expect(widget).toEqual( |
| 30 | + expect.objectContaining({ |
| 31 | + $$type: 'ais.chat', |
| 32 | + init: expect.any(Function), |
| 33 | + render: expect.any(Function), |
| 34 | + dispose: expect.any(Function), |
| 35 | + }) |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('Renders during init and render', () => { |
| 40 | + const renderFn = jest.fn(); |
| 41 | + const makeWidget = connectChat(renderFn); |
| 42 | + const widget = makeWidget({ agentId: 'agentId' }); |
| 43 | + |
| 44 | + // test if widget is not rendered yet at this point |
| 45 | + expect(renderFn).toHaveBeenCalledTimes(0); |
| 46 | + |
| 47 | + const helper = algoliasearchHelper(createSearchClient(), '', {}); |
| 48 | + helper.search = jest.fn(); |
| 49 | + |
| 50 | + widget.init(createInitOptions({ helper, state: helper.state })); |
| 51 | + |
| 52 | + expect(renderFn).toHaveBeenCalledTimes(1); |
| 53 | + expect(renderFn).toHaveBeenLastCalledWith( |
| 54 | + expect.objectContaining({ widgetParams: { agentId: 'agentId' } }), |
| 55 | + true |
| 56 | + ); |
| 57 | + |
| 58 | + const renderOptions = createRenderOptions({ helper }); |
| 59 | + widget.render(renderOptions); |
| 60 | + |
| 61 | + expect(renderFn).toHaveBeenCalledTimes(2); |
| 62 | + expect(renderFn).toHaveBeenLastCalledWith( |
| 63 | + expect.objectContaining({ widgetParams: { agentId: 'agentId' } }), |
| 64 | + false |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('dispose', () => { |
| 69 | + it('calls the unmount function', () => { |
| 70 | + const unmountFn = jest.fn(); |
| 71 | + const makeWidget = connectChat(() => {}, unmountFn); |
| 72 | + const widget = makeWidget({ agentId: 'agentId' }); |
| 73 | + |
| 74 | + expect(unmountFn).toHaveBeenCalledTimes(0); |
| 75 | + |
| 76 | + widget.dispose(); |
| 77 | + expect(unmountFn).toHaveBeenCalledTimes(1); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not throw without the unmount function', () => { |
| 81 | + const makeWidget = connectChat(() => {}); |
| 82 | + const widget = makeWidget({ agentId: 'agentId' }); |
| 83 | + |
| 84 | + expect(() => widget.dispose()).not.toThrow(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments