|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent, screen } from '@testing-library/react'; |
| 3 | +import { UseSupportCreateSuggestion } from './useSupportCreateSuggestion.stories'; |
| 4 | + |
| 5 | +describe('useSupportCreateSuggestion', () => { |
| 6 | + it('returns a createId and a createHintId', async () => { |
| 7 | + render(<UseSupportCreateSuggestion />); |
| 8 | + await screen.findByText('"createId": "@@ra-create"', { exact: false }); |
| 9 | + await screen.findByText('"createHintId": "@@ra-create-hint"', { |
| 10 | + exact: false, |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + it('returns a custom createId when createValue is provided', async () => { |
| 15 | + render(<UseSupportCreateSuggestion createValue="@@my-id" />); |
| 16 | + await screen.findByText('"createId": "@@my-id"', { exact: false }); |
| 17 | + await screen.findByText('"createHintId": "@@ra-create-hint"', { |
| 18 | + exact: false, |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns a custom createHintId when createHintValue is provided', async () => { |
| 23 | + render(<UseSupportCreateSuggestion createHintValue="@@my-id" />); |
| 24 | + await screen.findByText('"createId": "@@ra-create"', { exact: false }); |
| 25 | + await screen.findByText('"createHintId": "@@my-id"', { |
| 26 | + exact: false, |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns a createItem with id and name', async () => { |
| 31 | + render(<UseSupportCreateSuggestion />); |
| 32 | + await screen.findByText('"id": "@@ra-create"', { exact: false }); |
| 33 | + await screen.findByText('"name": "ra.action.create"', { |
| 34 | + exact: false, |
| 35 | + }); |
| 36 | + await screen.findByText('"disabled": false', { exact: false }); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns a createItem with id and label when optionText is 'label'", async () => { |
| 40 | + render(<UseSupportCreateSuggestion optionText="label" />); |
| 41 | + await screen.findByText('"id": "@@ra-create"', { exact: false }); |
| 42 | + await screen.findByText('"label": "ra.action.create"', { |
| 43 | + exact: false, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns a createItem with custom name when createLabel is provided', async () => { |
| 48 | + render(<UseSupportCreateSuggestion createLabel="Create a new item" />); |
| 49 | + await screen.findByText('"id": "@@ra-create"', { exact: false }); |
| 50 | + await screen.findByText('"name": "Create a new item"', { |
| 51 | + exact: false, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns a hint as createItem when createItemLabel is provided and no filter is set', async () => { |
| 56 | + render( |
| 57 | + <UseSupportCreateSuggestion createItemLabel="Create a new item called %{item}" /> |
| 58 | + ); |
| 59 | + await screen.findByText('"id": "@@ra-create-hint"', { exact: false }); |
| 60 | + await screen.findByText('"name": "ra.action.create"', { |
| 61 | + exact: false, |
| 62 | + }); |
| 63 | + await screen.findByText('"disabled": true', { exact: false }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('uses the filter in the createItem name when createItemLabel is provided', async () => { |
| 67 | + render( |
| 68 | + <UseSupportCreateSuggestion createItemLabel="Create a new item called %{item}" /> |
| 69 | + ); |
| 70 | + fireEvent.change(await screen.findByLabelText('Autocomplete filter'), { |
| 71 | + target: { value: 'foo' }, |
| 72 | + }); |
| 73 | + await screen.findByText('"id": "@@ra-create"', { exact: false }); |
| 74 | + await screen.findByText('"name": "Create a new item called foo"', { |
| 75 | + exact: false, |
| 76 | + }); |
| 77 | + await screen.findByText('"disabled": false', { exact: false }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('calls onCreate with the filter when the createItem is clicked', async () => { |
| 81 | + render(<UseSupportCreateSuggestion />); |
| 82 | + fireEvent.change(await screen.findByLabelText('Autocomplete filter'), { |
| 83 | + target: { value: 'foo' }, |
| 84 | + }); |
| 85 | + fireEvent.click(await screen.findByText('Simulate click on create')); |
| 86 | + await screen.findByText('"foo"'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('renders the create element with the filter as default value when the createItem is clicked', async () => { |
| 90 | + render(<UseSupportCreateSuggestion withCreateElement />); |
| 91 | + fireEvent.change(await screen.findByLabelText('Autocomplete filter'), { |
| 92 | + target: { value: 'foo' }, |
| 93 | + }); |
| 94 | + fireEvent.click(await screen.findByText('Simulate click on create')); |
| 95 | + await screen.findByLabelText('resources.authors.fields.foo'); |
| 96 | + await screen.findByLabelText('resources.authors.fields.bar'); |
| 97 | + // We expect 2 inputs with value 'foo': the filter input and the input in the create element |
| 98 | + expect(screen.getAllByDisplayValue('foo')).toHaveLength(2); |
| 99 | + }); |
| 100 | + |
| 101 | + it("calls create with the new element's data when the create element's form is submitted", async () => { |
| 102 | + render(<UseSupportCreateSuggestion withCreateElement />); |
| 103 | + fireEvent.change(await screen.findByLabelText('Autocomplete filter'), { |
| 104 | + target: { value: 'foo' }, |
| 105 | + }); |
| 106 | + fireEvent.click(await screen.findByText('Simulate click on create')); |
| 107 | + fireEvent.change( |
| 108 | + await screen.findByLabelText('resources.authors.fields.bar'), |
| 109 | + { target: { value: 'baz' } } |
| 110 | + ); |
| 111 | + fireEvent.click(await screen.findByText('ra.action.save')); |
| 112 | + await screen.findByText('"foo": "foo"', { exact: false }); |
| 113 | + await screen.findByText('"bar": "baz"', { exact: false }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('hides the create element when the cancel button is clicked', async () => { |
| 117 | + render(<UseSupportCreateSuggestion withCreateElement />); |
| 118 | + fireEvent.click(await screen.findByText('Simulate click on create')); |
| 119 | + await screen.findByLabelText('resources.authors.fields.foo'); |
| 120 | + await screen.findByLabelText('resources.authors.fields.bar'); |
| 121 | + fireEvent.click(await screen.findByText('Cancel')); |
| 122 | + expect( |
| 123 | + screen.queryByLabelText('resources.authors.fields.foo') |
| 124 | + ).toBeNull(); |
| 125 | + expect( |
| 126 | + screen.queryByLabelText('resources.authors.fields.bar') |
| 127 | + ).toBeNull(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments