Skip to content

Commit

Permalink
Update tests with realistic delay
Browse files Browse the repository at this point in the history
Signed-off-by: Frances Thai <[email protected]>
  • Loading branch information
notfelineit committed Nov 5, 2024
1 parent b367f05 commit 233ca0f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
2 changes: 1 addition & 1 deletion web/vtadmin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"eslint-config-react-app": "^7.0.1",
"i": "^0.3.7",
"jsdom": "^21.1.1",
"msw": "^2.5.2",
"msw": "^0.36.8",
"npm": "^10.8.0",
"postcss": "^8.4.31",
"prettier": "^2.2.1",
Expand Down
15 changes: 6 additions & 9 deletions web/vtadmin/src/components/ActionPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { delay, http, HttpResponse } from 'msw';
import { QueryClient, QueryClientProvider, useMutation } from 'react-query';
import { describe, it, expect, vi } from 'vitest';

Expand All @@ -33,11 +33,7 @@ describe('ActionPanel', () => {
* provides such a function and should be `render`ed in the context QueryClientProvider.
*/
const Wrapper: React.FC<ActionPanelProps & { url: string }> = (props) => {
const mutation = useMutation(() => fetch(new URL(props['url']), { method: 'post' }), {
onError: (error) => {
console.log('ERROR: ', error);
},
});
const mutation = useMutation(() => fetch(new URL(props['url']), { method: 'post' }));
return <ActionPanel {...props} mutation={mutation as any} />;
};

Expand All @@ -46,7 +42,8 @@ describe('ActionPanel', () => {

const url = `${import.meta.env.VITE_VTADMIN_API_ADDRESS}/api/test`;
global.server.use(
http.post(url, (info) => {
http.post(url, async (info) => {
await delay()
return HttpResponse.json({ ok: true });
})
);
Expand All @@ -73,8 +70,8 @@ describe('ActionPanel', () => {
// Enter the confirmation text
await user.type(input, 'zone1-101');
expect(button).not.toHaveAttribute('disabled');

await user.click(button);
await user.click(button)

// Validate form while API request is in flight
expect(button).toHaveTextContent('Doing Action...');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { delay, http, HttpResponse } from 'msw';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { createMemoryHistory } from 'history';
Expand All @@ -32,23 +31,18 @@ import * as Snackbar from '../../Snackbar';
// about UI structure (boo!), which means this test is rather brittle
// to UI changes (e.g., like how the Select works, adding new form fields, etc.)
describe('CreateKeyspace integration test', () => {
const server = setupServer();

afterAll(() => {
server.close();
});

it('successfully creates a keyspace', async () => {
vi.spyOn(global, 'fetch');
vi.spyOn(Snackbar, 'success');

const cluster = { id: 'local', name: 'local' };
const apiAddr = import.meta.env.VITE_VTADMIN_API_ADDRESS;
server.use(
http.get(`${apiAddr}/api/clusters`, (info) => {
global.server.use(
http.get(`${apiAddr}/api/clusters`, async (info) => {
return HttpResponse.json({ result: { clusters: [cluster] }, ok: true });
}),
http.post(`${apiAddr}/api/keyspace/:clusterID`, (info) => {
http.post(`${apiAddr}/api/keyspace/:clusterID`, async (info) => {
await delay()
const data: vtadmin.ICreateKeyspaceResponse = {
keyspace: {
cluster: { id: cluster.id, name: cluster.name },
Expand All @@ -58,7 +52,6 @@ describe('CreateKeyspace integration test', () => {
return HttpResponse.json({ result: data, ok: true });
})
);
server.listen();

const history = createMemoryHistory();
vi.spyOn(history, 'push');
Expand Down

0 comments on commit 233ca0f

Please sign in to comment.