Skip to content

Commit

Permalink
Revert Inventory groups back end mocks (#1845)
Browse files Browse the repository at this point in the history
* Revert "chore: Mock GET groups related requests (#1793)"

This reverts commit a82ed4f.

* perPage and hostname fixes
  • Loading branch information
gkarat authored Apr 27, 2023
1 parent dd4efac commit e633f77
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 85 deletions.
37 changes: 0 additions & 37 deletions cypress/fixtures/groups/Ba8B79ab5adC8E41e255D5f8aDb8f1F3.json

This file was deleted.

2 changes: 1 addition & 1 deletion cypress/support/interceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const groupsInterceptors = {
.as('getGroups'),
'successful with some items second page': () =>
cy
.intercept('GET', '/api/inventory/v1/groups?*page=2&perPage=50*', {
.intercept('GET', '/api/inventory/v1/groups?*page=2&per_page=50*', {
statusCode: 200,
body: groupsSecondPage
})
Expand Down
3 changes: 2 additions & 1 deletion src/Utilities/hooks/useFetchBatched.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const useFetchBatched = () => {

const results = resolve(
[...new Array(pages)].map(
(_, pageIdx) => () => fetchFunction(filter, { page: pageIdx + 1, perPage: batchSize })
// eslint-disable-next-line camelcase
(_, pageIdx) => () => fetchFunction(filter, { page: pageIdx + 1, per_page: batchSize })
)
);

Expand Down
8 changes: 4 additions & 4 deletions src/components/GroupsTable/GroupsTable.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('pagination', () => {
changePagination(el).then(() => {
cy.wait('@getGroups')
.its('request.url')
.should('include', `perPage=${el}`);
.should('include', `per_page=${el}`);
});
});
});
Expand Down Expand Up @@ -186,20 +186,20 @@ describe('filtering', () => {

it('sends correct request', () => {
applyNameFilter();
cy.wait('@getGroups').its('request.url').should('include', 'hostname_or_id=lorem');
cy.wait('@getGroups').its('request.url').should('include', 'name=lorem');
});

it('can remove the chip or reset filters', () => {
applyNameFilter();
cy.wait('@getGroups').its('request.url').should('contain', 'hostname_or_id=lorem');
cy.wait('@getGroups').its('request.url').should('contain', 'name=lorem');
cy.get(CHIP_GROUP)
.find(CHIP)
.ouiaId('close', 'button')
.each(() => {
cy.get(CHIP_GROUP).find(CHIP).ouiaId('close', 'button');
});
cy.get('button').contains('Reset filters').click();
cy.wait('@getGroups').its('request.url').should('not.contain', 'hostname_or_id');
cy.wait('@getGroups').its('request.url').should('not.contain', 'name');
cy.get(CHIP_GROUP).should('not.exist');

});
Expand Down
20 changes: 10 additions & 10 deletions src/components/GroupsTable/GroupsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ const GroupsTable = () => {
const order_by = GROUPS_TABLE_COLUMNS_TO_URL[sortIndex];
const order_how = upperCase(sortDirection);
return dispatch(
fetchGroups({ ...search, order_by, order_how }, { page, perPage })
fetchGroups({ ...search, order_by, order_how }, { page, per_page: perPage })
);
} else {
return dispatch(fetchGroups(search, { page, perPage }));
return dispatch(fetchGroups(search, { page, per_page: perPage }));
}
}, REQUEST_DEBOUNCE_TIMEOUT), // wait the timeout before making the final fetch
[]
Expand Down Expand Up @@ -140,16 +140,16 @@ const GroupsTable = () => {
data-ouia-component-type="PF4/TextInput"
data-ouia-component-id="name-filter"
placeholder="Filter by name"
value={filters.hostname_or_id || ''}
value={filters.name || ''}
onChange={(value) => {
const { hostname_or_id, ...fs } = filters;
const { name, ...fs } = filters;
return setFilters({
...fs,
...(value.length > 0 ? { hostname_or_id: value } : {})
...(value.length > 0 ? { name: value } : {})
});
}}
onClear={() => {
const { hostname_or_id, ...fs } = filters;
const { name, ...fs } = filters;
return setFilters(fs);
}}
isDisabled={rejected}
Expand All @@ -158,20 +158,20 @@ const GroupsTable = () => {
}
}
],
[filters.hostname_or_id, rejected]
[filters.name, rejected]
);

const onResetFilters = () => setFilters(GROUPS_TABLE_INITIAL_STATE);

const activeFiltersConfig = {
showDeleteButton: !!filters.hostname_or_id,
showDeleteButton: !!filters.name,
deleteTitle: 'Reset filters',
filters: filters.hostname_or_id
filters: filters.name
? [
{
category: 'Name',
chips: [
{ name: filters.hostname_or_id, value: filters.hostname_or_id }
{ name: filters.name, value: filters.name }
]
}
]
Expand Down
39 changes: 10 additions & 29 deletions src/components/InventoryGroups/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@ import { INVENTORY_API_BASE } from '../../../api';
import { TABLE_DEFAULT_PAGINATION } from '../../../constants';
import PropTypes from 'prop-types';
import union from 'lodash/union';
import fixtureGroups from '../../../../cypress/fixtures/groups.json';
import fixtureGroupsDetails from '../../../../cypress/fixtures/groups/Ba8B79ab5adC8E41e255D5f8aDb8f1F3.json';

// eslint-disable-next-line camelcase
export const getGroups = (search = {}, pagination = { page: 1, per_page: TABLE_DEFAULT_PAGINATION }) => {
if (window.Cypress) {
const parameters = new URLSearchParams({
...search,
...pagination
}).toString();
const parameters = new URLSearchParams({
...search,
...pagination
}).toString();

return instance.get(`${INVENTORY_API_BASE}/groups?${parameters}`);
}

// FIXME: remove mock data when API is implemented
return Promise.resolve(fixtureGroups);
return instance.get(`${INVENTORY_API_BASE}/groups?${parameters}` /* , { headers: { Prefer: 'code=404' } } */);
};

export const createGroup = (payload) => {
Expand All @@ -31,22 +23,12 @@ export const createGroup = (payload) => {
};

export const validateGroupName = (name) => {
if (window.Cypress) {
return instance.get(`${INVENTORY_API_BASE}/groups`)
.then((resp) => resp?.results.some((group) => group.name === name));
}

// FIXME: remove mock data when API is implemented
return Promise.resolve(fixtureGroups).then((resp) => resp?.results.some((group) => group.name === name));
return instance.get(`${INVENTORY_API_BASE}/groups`)
.then((resp) => resp?.results.some((group) => group.name === name));
};

export const getGroupDetail = (groupId) => {
if (window.Cypress) {
return instance.get(`${INVENTORY_API_BASE}/groups/${groupId}`);
}

// FIXME: remove mock data when API is implemented
return Promise.resolve(fixtureGroupsDetails);
return instance.get(`${INVENTORY_API_BASE}/groups/${groupId}`);
};

export const updateGroupById = (id, payload) => {
Expand Down Expand Up @@ -75,11 +57,10 @@ export const addHostToGroup = (groupId, newHostId) => {

getGroups.propTypes = {
search: PropTypes.shape({
// eslint-disable-next-line camelcase
hostname_or_id: PropTypes.string
name: PropTypes.string
}),
pagination: PropTypes.shape({
perPage: PropTypes.number,
per_page: PropTypes.number,
page: PropTypes.number
})
};
Expand Down
6 changes: 3 additions & 3 deletions src/store/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { editAnsibleHost, editDisplayName, fetchGroups, systemProfile } from './actions';
import { hosts } from '../api';
import mockedData from '../__mocks__/mockedData.json';
import fixturesGroups from '../../cypress/fixtures/groups.json';
import mockedGroups from '../__mocks__/mockedGroups.json';
import MockAdapter from 'axios-mock-adapter';

const mocked = new MockAdapter(hosts.axios);
Expand Down Expand Up @@ -65,10 +65,10 @@ describe('editAnsibleHost', () => {
describe('fetchGroups', () => {
it('should call correct endpoint', async () => {
mocked.onGet(new RegExp('/api/inventory/v1/groups*')).reply(() => {
return [200, fixturesGroups];
return [200, mockedGroups];
});
const { type, payload } = await fetchGroups();
expect(type).toBe('GROUPS');
expect(await payload).toEqual(fixturesGroups);
expect(await payload).toEqual(mockedGroups);
});
});

0 comments on commit e633f77

Please sign in to comment.