|
11 | 11 |
|
12 | 12 | import {createTest, initComponent, shutdownTest} from '../tools'; |
13 | 13 | import {getByTestId, getByText, waitFor} from '@testing-library/dom'; |
| 14 | +import userEvent from "@testing-library/user-event"; |
14 | 15 |
|
15 | 16 | describe('LiveController data-loading Tests', () => { |
16 | 17 | afterEach(() => { |
@@ -50,7 +51,6 @@ describe('LiveController data-loading Tests', () => { |
50 | 51 | }); |
51 | 52 |
|
52 | 53 | it('takes into account the "action" modifier', async () => { |
53 | | - // TODO start here! |
54 | 54 | const test = await createTest({}, (data: any) => ` |
55 | 55 | <div ${initComponent(data)}> |
56 | 56 | <span data-loading="action(save)|show" data-testid="loading-element">Loading...</span> |
@@ -97,8 +97,60 @@ describe('LiveController data-loading Tests', () => { |
97 | 97 | expect(getByTestId(test.element, 'loading-element')).not.toBeVisible(); |
98 | 98 | }); |
99 | 99 |
|
| 100 | + it('takes into account the "model" modifier', async () => { |
| 101 | + const test = await createTest({ comments: '', user: { email: '' }}, (data: any) => ` |
| 102 | + <div ${initComponent(data)}> |
| 103 | + <textarea data-model="comments"></textarea> |
| 104 | + <span data-loading="model(comments)|show" data-testid="comments-loading">Comments change loading...</span> |
| 105 | +
|
| 106 | + <textarea data-model="user.email"></textarea> |
| 107 | + <span data-loading="model(user.email)|show" data-testid="email-loading">Checking if email is taken...</span> |
| 108 | + </div> |
| 109 | + `); |
| 110 | + |
| 111 | + test.expectsAjaxCall('get') |
| 112 | + .expectSentData({ comments: 'Changing the comments!', user: { email: '' } }) |
| 113 | + // delay so we can check loading |
| 114 | + .delayResponse(50) |
| 115 | + .init(); |
| 116 | + |
| 117 | + userEvent.type(test.queryByDataModel('comments'), 'Changing the comments!') |
| 118 | + // it should not be loading yet due to debouncing |
| 119 | + expect(getByTestId(test.element, 'comments-loading')).not.toBeVisible(); |
| 120 | + // wait for ajax call to start |
| 121 | + await waitFor(() => expect(test.element).toHaveAttribute('busy')); |
| 122 | + // NOW it should be loading |
| 123 | + expect(getByTestId(test.element, 'comments-loading')).toBeVisible(); |
| 124 | + // but email-loading is not loading |
| 125 | + expect(getByTestId(test.element, 'email-loading')).not.toBeVisible(); |
| 126 | + // wait for Ajax call to finish |
| 127 | + await waitFor(() => expect(test.element).not.toHaveAttribute('busy')); |
| 128 | + // loading is no longer visible |
| 129 | + expect(getByTestId(test.element, 'comments-loading')).not.toBeVisible(); |
| 130 | + |
| 131 | + // now try the user.email "child property" field |
| 132 | + test.expectsAjaxCall('get') |
| 133 | + .expectSentData({ comments: 'Changing the comments!', user: { email: '[email protected]' } }) |
| 134 | + // delay so we can check loading |
| 135 | + .delayResponse(50) |
| 136 | + .init(); |
| 137 | + |
| 138 | + userEvent.type(test.queryByDataModel('user.email'), '[email protected]'); |
| 139 | + // it should not be loading yet due to debouncing |
| 140 | + expect(getByTestId(test.element, 'email-loading')).not.toBeVisible(); |
| 141 | + // wait for ajax call to start |
| 142 | + await waitFor(() => expect(test.element).toHaveAttribute('busy')); |
| 143 | + // NOW it should be loading |
| 144 | + expect(getByTestId(test.element, 'email-loading')).toBeVisible(); |
| 145 | + // but comments-loading is not loading |
| 146 | + expect(getByTestId(test.element, 'comments-loading')).not.toBeVisible(); |
| 147 | + // wait for Ajax call to finish |
| 148 | + await waitFor(() => expect(test.element).not.toHaveAttribute('busy')); |
| 149 | + // loading is no longer visible |
| 150 | + expect(getByTestId(test.element, 'email-loading')).not.toBeVisible(); |
| 151 | + }); |
| 152 | + |
100 | 153 | it('can handle multiple actions on the same request', async () => { |
101 | | - // TODO start here! |
102 | 154 | const test = await createTest({}, (data: any) => ` |
103 | 155 | <div ${initComponent(data)}> |
104 | 156 | <span data-loading="action(otherAction)|show" data-testid="loading-element">Loading...</span> |
|
0 commit comments