debian-mirror-gitlab/spec/frontend/admin/users/components/users_table_spec.js

147 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-06-08 01:23:25 +05:30
import { GlTable, GlSkeletonLoader } from '@gitlab/ui';
2022-01-26 12:08:38 +05:30
import Vue from 'vue';
2021-06-08 01:23:25 +05:30
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
2022-04-04 11:22:00 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2021-06-08 01:23:25 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
import AdminUserActions from '~/admin/users/components/user_actions.vue';
2021-03-08 18:12:59 +05:30
import AdminUserAvatar from '~/admin/users/components/user_avatar.vue';
2021-03-11 19:13:27 +05:30
import AdminUsersTable from '~/admin/users/components/users_table.vue';
2021-06-08 01:23:25 +05:30
import getUsersGroupCountsQuery from '~/admin/users/graphql/queries/get_users_group_counts.query.graphql';
import createFlash from '~/flash';
2021-04-29 21:17:54 +05:30
import AdminUserDate from '~/vue_shared/components/user_date.vue';
2021-03-11 19:13:27 +05:30
2021-06-08 01:23:25 +05:30
import { users, paths, createGroupCountResponse } from '../mock_data';
jest.mock('~/flash');
2022-01-26 12:08:38 +05:30
Vue.use(VueApollo);
2021-02-22 17:27:13 +05:30
describe('AdminUsersTable component', () => {
let wrapper;
2021-06-08 01:23:25 +05:30
const user = users[0];
2021-02-22 17:27:13 +05:30
2021-06-08 01:23:25 +05:30
const createFetchGroupCount = (data) =>
jest.fn().mockResolvedValue(createGroupCountResponse(data));
const fetchGroupCountsLoading = jest.fn().mockResolvedValue(new Promise(() => {}));
const fetchGroupCountsError = jest.fn().mockRejectedValue(new Error('Network error'));
const fetchGroupCountsResponse = createFetchGroupCount([{ id: user.id, groupCount: 5 }]);
const findUserGroupCount = (id) => wrapper.findByTestId(`user-group-count-${id}`);
const findUserGroupCountLoader = (id) => findUserGroupCount(id).find(GlSkeletonLoader);
2021-02-22 17:27:13 +05:30
const getCellByLabel = (trIdx, label) => {
return wrapper
.find(GlTable)
.find('tbody')
.findAll('tr')
.at(trIdx)
.find(`[data-label="${label}"][role="cell"]`);
};
2021-06-08 01:23:25 +05:30
function createMockApolloProvider(resolverMock) {
const requestHandlers = [[getUsersGroupCountsQuery, resolverMock]];
return createMockApollo(requestHandlers);
}
const initComponent = (props = {}, resolverMock = fetchGroupCountsResponse) => {
wrapper = mountExtended(AdminUsersTable, {
apolloProvider: createMockApolloProvider(resolverMock),
2021-02-22 17:27:13 +05:30
propsData: {
users,
paths,
...props,
},
});
};
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
describe('when there are users', () => {
beforeEach(() => {
initComponent();
});
2021-03-11 19:13:27 +05:30
it('renders the projects count', () => {
expect(getCellByLabel(0, 'Projects').text()).toContain(`${user.projectsCount}`);
2021-03-08 18:12:59 +05:30
});
2021-03-11 19:13:27 +05:30
it('renders the user actions', () => {
expect(wrapper.find(AdminUserActions).exists()).toBe(true);
});
it.each`
component | label
${AdminUserAvatar} | ${'Name'}
${AdminUserDate} | ${'Created on'}
${AdminUserDate} | ${'Last activity'}
`('renders the component for column $label', ({ component, label }) => {
expect(getCellByLabel(0, label).find(component).exists()).toBe(true);
2021-02-22 17:27:13 +05:30
});
});
describe('when users is an empty array', () => {
beforeEach(() => {
initComponent({ users: [] });
});
it('renders a "No users found" message', () => {
expect(wrapper.text()).toContain('No users found');
});
});
2021-06-08 01:23:25 +05:30
describe('group counts', () => {
describe('when fetching the data', () => {
beforeEach(() => {
initComponent({}, fetchGroupCountsLoading);
});
it('renders a loader for each user', () => {
expect(findUserGroupCountLoader(user.id).exists()).toBe(true);
});
});
describe('when the data has been fetched', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2021-06-08 01:23:25 +05:30
initComponent();
2022-04-04 11:22:00 +05:30
await waitForPromises();
2021-06-08 01:23:25 +05:30
});
it("renders the user's group count", () => {
expect(findUserGroupCount(user.id).text()).toBe('5');
});
describe("and a user's group count is null", () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2021-06-08 01:23:25 +05:30
initComponent({}, createFetchGroupCount([{ id: user.id, groupCount: null }]));
2022-04-04 11:22:00 +05:30
await waitForPromises();
2021-06-08 01:23:25 +05:30
});
it("renders the user's group count as 0", () => {
expect(findUserGroupCount(user.id).text()).toBe('0');
});
});
});
describe('when there is an error while fetching the data', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2021-06-08 01:23:25 +05:30
initComponent({}, fetchGroupCountsError);
2022-04-04 11:22:00 +05:30
await waitForPromises();
2021-06-08 01:23:25 +05:30
});
it('creates a flash message and captures the error', () => {
expect(createFlash).toHaveBeenCalledWith({
message: 'Could not load user group counts. Please refresh the page to try again.',
captureError: true,
error: expect.any(Error),
});
});
});
});
2021-02-22 17:27:13 +05:30
});