|
| 1 | +import { getBackendSrv } from '@grafana/runtime'; |
| 2 | +import { Button, Collapse, Field } from '@grafana/ui'; |
| 3 | +import { mount, shallow } from 'enzyme'; |
| 4 | +import React from 'react'; |
| 5 | +import { Editor, EditorProps } from './editor'; |
| 6 | +jest.mock('@grafana/runtime'); |
| 7 | + |
| 8 | +describe('editor', () => { |
| 9 | + test('add button', () => { |
| 10 | + const mockOnChange = jest.fn(); |
| 11 | + const props: EditorProps = { |
| 12 | + buttons: [], |
| 13 | + onChange: mockOnChange, |
| 14 | + }; |
| 15 | + const wrapper = shallow(<Editor {...props} />); |
| 16 | + expect(wrapper.find(Collapse)).toHaveLength(0); |
| 17 | + const field = wrapper.find(Field); |
| 18 | + expect(field).toHaveLength(1); |
| 19 | + expect(field.children).toHaveLength(1); |
| 20 | + const button = field.find(Button); |
| 21 | + expect(button).toHaveLength(1); |
| 22 | + expect(button.prop('variant')).toBe('secondary'); |
| 23 | + expect(button.text()).toBe('Add Button'); |
| 24 | + expect(button.prop('icon')).toBe('plus'); |
| 25 | + expect(button.prop('size')).toBe('sm'); |
| 26 | + button.simulate('click'); |
| 27 | + expect(mockOnChange).toHaveBeenCalledWith([ |
| 28 | + { text: '', datasource: '', query: '' }, |
| 29 | + ]); |
| 30 | + }); |
| 31 | + |
| 32 | + test('button collapse', () => { |
| 33 | + const mockOnChange = jest.fn(); |
| 34 | + const props: EditorProps = { |
| 35 | + buttons: [{ text: 'a' }, { text: 'b' }], |
| 36 | + onChange: mockOnChange, |
| 37 | + }; |
| 38 | + |
| 39 | + const mockGet = jest.fn().mockReturnValue([{ name: 'a' }]); |
| 40 | + getBackendSrv.mockImplementation(() => ({ get: mockGet })); |
| 41 | + |
| 42 | + const wrapper = mount(<Editor {...props} />); |
| 43 | + expect(wrapper.children()).toHaveLength(3); |
| 44 | + const collapse = wrapper.find(Collapse); |
| 45 | + expect(collapse).toHaveLength(2); |
| 46 | + |
| 47 | + collapse.forEach((c, i) => { |
| 48 | + expect(c.key()).toBe(i.toString()); |
| 49 | + expect(c.prop('label')).toBe('Button ' + (i + 1).toString()); |
| 50 | + expect(c.prop('collapsible')).toBeTruthy(); |
| 51 | + expect(c.prop('isOpen')).toBeFalsy(); |
| 52 | + // c.simulate('toggle'); |
| 53 | + // expect(c.prop('isOpen')).toBeTruthy(); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments