Skip to content

Commit

Permalink
fix: test cases and snapshots updated
Browse files Browse the repository at this point in the history
  • Loading branch information
sghosh-c-eightfold committed Jan 15, 2025
1 parent 8d00e53 commit c04fa93
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 176 deletions.
13 changes: 9 additions & 4 deletions src/components/InlineSvg/InlineSvg.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ describe('InlineSvg', () => {

test('renders an error icon when the SVG image fails to load', async () => {
const errorUrl = 'https://example.com/broken.svg';
// Mock fetch to simulate a failed request
global.fetch = jest.fn(() =>
Promise.reject(new Error('Failed to fetch'))
) as jest.Mock;

const { container } = render(
<InlineSvg url={errorUrl} width={width} height={height} />
);

// Wait for error state to be set and component to re-render
await waitFor(() => {
expect(
container.querySelector('.svg-display-error-icon')
).toBeInTheDocument();
});
expect(container.querySelector('.svg-display-error-icon')).toBeInTheDocument();
}, { timeout: 3000 });
});

test('Should call fetchSvg only once', async () => {
Expand Down
137 changes: 53 additions & 84 deletions src/components/Table/Tests/Table.sorter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('Table.sorter', () => {
const getNameColumn = () => wrapper.find('th').at(0);

expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();
});

it('should change aria-sort when default sort order is set to descend', () => {
Expand All @@ -114,13 +114,14 @@ describe('Table.sorter', () => {

// Test that it cycles through the order of sortDirections
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();

wrapper.find('.table-column-sorters').simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual('ascending');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeTruthy();

wrapper.find('.table-column-sorters').simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
});

it('sort records', () => {
Expand All @@ -131,17 +132,18 @@ describe('Table.sorter', () => {

// first assert default state
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();

// ascend
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
expect(getNameColumn().prop('aria-sort')).toEqual('ascending');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeTruthy();

// descend
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();
});

it('sort records with keydown', () => {
Expand Down Expand Up @@ -341,13 +343,15 @@ describe('Table.sorter', () => {

// sort name
getNameSortButton().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual('ascending');
expect(getAgeColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeTruthy();
expect(getAgeColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getAgeColumn().find('.table-column-sorter-down').exists()).toBeFalsy();

// sort age
getAgeSortButton().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getAgeColumn().prop('aria-sort')).toEqual('ascending');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
expect(getAgeColumn().find('.table-column-sorter-up').exists()).toBeTruthy();
});

it('should toggle sort state when columns are put in render', () => {
Expand Down Expand Up @@ -393,15 +397,16 @@ describe('Table.sorter', () => {

// sort name
getSortButton().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual('ascending');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeTruthy();

// sort name
getSortButton().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();

// sort name
getSortButton().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
});

it('should toggle sort state when columns with non primitive properties are put in render', () => {
Expand Down Expand Up @@ -445,18 +450,20 @@ describe('Table.sorter', () => {
const wrapper = mount(<TableTest />);

const getNameColumn = () => wrapper.find('th').at(0);
const getSortButton = () => wrapper.find('.table-column-sorters').at(0);

// sort name
getNameColumn().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual('ascending');
getSortButton().simulate('click');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeTruthy();

// sort name
getNameColumn().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
getSortButton().simulate('click');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();

// sort name
getNameColumn().simulate('click');
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
getSortButton().simulate('click');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
});

it('should toggle sort state when columns with key are put in render', () => {
Expand Down Expand Up @@ -501,67 +508,24 @@ describe('Table.sorter', () => {

const wrapper = mount(<TableTest />);
const getNameColumn = () => wrapper.find('th').at(0);
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-up')
).toBeFalsy();
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-down')
).toBeFalsy();
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
const getSortButton = () => wrapper.find('.table-column-sorters').at(0);

// Initial state
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();

// sort name
getNameColumn().simulate('click');
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-up')
).toBeTruthy();
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-down')
).toBeFalsy();
expect(getNameColumn().prop('aria-sort')).toEqual('ascending');
getSortButton().simulate('click');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeTruthy();

// sort name
getNameColumn().simulate('click');
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-up')
).toBeFalsy();
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-down')
).toBeTruthy();
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
getSortButton().simulate('click');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();

// sort name
getNameColumn().simulate('click');
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-up')
).toBeFalsy();
expect(
getNameColumn()
.find('.table-column-sorter')
.at(0)
.hasClass('table-column-sorter-down')
).toBeFalsy();
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
getSortButton().simulate('click');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
});

it('should first sort by descend, then ascend, then cancel sort', () => {
Expand All @@ -576,17 +540,18 @@ describe('Table.sorter', () => {
// descend
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();

// ascend
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
expect(getNameColumn().prop('aria-sort')).toEqual('ascending');
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeTruthy();

// cancel sort
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
});

it('should first sort by descend, then cancel sort', () => {
Expand All @@ -600,17 +565,19 @@ describe('Table.sorter', () => {
const getSortButton = () => wrapper.find('.table-column-sorters').at(0);

// default
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();

// descend
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();

// cancel sort
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
});

it('should first sort by descend, then cancel sort. (column prop)', () => {
Expand All @@ -627,17 +594,19 @@ describe('Table.sorter', () => {
const getSortButton = () => wrapper.find('.table-column-sorters').at(0);

// default
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();

// descend
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual('descending');
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeTruthy();

// cancel sort
getSortButton().simulate('click');
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
expect(getNameColumn().prop('aria-sort')).toEqual(undefined);
expect(getNameColumn().find('.table-column-sorter-up').exists()).toBeFalsy();
expect(getNameColumn().find('.table-column-sorter-down').exists()).toBeFalsy();
});

it('should support onHeaderCell in sort column', () => {
Expand Down
Loading

0 comments on commit c04fa93

Please sign in to comment.