Skip to content

Commit

Permalink
ui/test: Add describe block for the tests to limit the scope of function
Browse files Browse the repository at this point in the history
To make sure the functions beforeAll, afterEach, afterAll function in
the correct place.

Refs: #2932
  • Loading branch information
ChengYanJin committed Feb 1, 2021
1 parent 39e63d4 commit 7c642f5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ui/src/components/NodePartitionTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ const NodePartitionTable = ({ instanceIP }: { instanceIP: string }) => {
percentage={cell.value}
buildinLabel={`${cell.value}%`}
backgroundColor={theme.brand.primaryDark1}
aria-label={'percentage'}
aria-label={`${cell.value}%`}
/>
</Cell>
);
Expand Down
105 changes: 56 additions & 49 deletions ui/src/components/NodePartitionTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,61 +90,68 @@ const server = setupServer(
),
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
describe('the system partition table', () => {
beforeAll(() => server.listen());

test('the partition table', async () => {
// Setup
test('displays the table', async () => {
// Setup

// use fake timers to let react query retry immediately after promise failure
jest.useFakeTimers();
initializeProm('http://192.168.1.18:8443/api/prometheus');
initializeAM('http://192.168.1.18:8443/api/alertmanager');
// use fake timers to let react query retry immediately after promise failure
jest.useFakeTimers();
initializeProm('http://192.168.1.18:8443/api/prometheus');
initializeAM('http://192.168.1.18:8443/api/alertmanager');

const { getByLabelText } = render(
<NodePartitionTable instanceIP={'192.168.1.29'} />,
);
expect(getByLabelText('loading')).toBeInTheDocument();
const { getByLabelText } = render(
<NodePartitionTable instanceIP={'192.168.1.29'} />,
);
expect(getByLabelText('loading')).toBeInTheDocument();

// Exercise
await waitForLoadingToFinish();
// Exercise
await waitForLoadingToFinish();

// Verify
expect(screen.getByLabelText('status warning')).toBeInTheDocument();
expect(screen.getByLabelText('percentage')).toBeInTheDocument();
});
// Verify
expect(screen.getByLabelText('status warning')).toBeInTheDocument();
expect(screen.getByLabelText('97%')).toBeInTheDocument();
expect(screen.getByText('/mnt/testpart')).toBeInTheDocument();
// since we use the same query, so the number of global size is the same as usage
expect(screen.getByText('97Bytes')).toBeInTheDocument();
});

afterEach(() => server.resetHandlers());

test('handles server error', async () => {
// S
jest.useFakeTimers();
initializeProm('http://192.168.1.18:8443/api/prometheus');
initializeAM('http://192.168.1.18:8443/api/alertmanager');
// override the default route with error status
server.use(
rest.get(
'http://192.168.1.18:8443/api/prometheus/api/v1/query',
(req, res, ctx) => {
return res(ctx.status(500));
},
),
rest.get(
'http://192.168.1.18:8443/api/alertmanager/api/v2/alerts',
(req, res, ctx) => {
return res(ctx.status(500));
},
),
);
const { getByLabelText } = render(
<NodePartitionTable instanceIP={'192.168.1.29'} />,
);
expect(getByLabelText('loading')).toBeInTheDocument();

test('handles server error', async () => {
// S
jest.useFakeTimers();
initializeProm('http://192.168.1.18:8443/api/prometheus');
initializeAM('http://192.168.1.18:8443/api/alertmanager');
// override the default route with error status
server.use(
rest.get(
'http://192.168.1.18:8443/api/prometheus/api/v1/query',
(req, res, ctx) => {
return res(ctx.status(500));
},
),
rest.get(
'http://192.168.1.18:8443/api/alertmanager/api/v2/alerts',
(req, res, ctx) => {
return res(ctx.status(500));
},
),
);
const { getByLabelText } = render(
<NodePartitionTable instanceIP={'192.168.1.29'} />,
);
expect(getByLabelText('loading')).toBeInTheDocument();
// E
await waitForLoadingToFinish();

// E
await waitForLoadingToFinish();
// V
expect(
screen.getByText('System partitions request has failed.'),
).toBeInTheDocument();
});

// V
expect(
screen.getByText('System partitions request has failed.'),
).toBeInTheDocument();
afterAll(() => server.close());
});

0 comments on commit 7c642f5

Please sign in to comment.