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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 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';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
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';
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2021-06-08 01:23:25 +05:30
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}`);
2022-08-27 11:52:29 +05:30
const findUserGroupCountLoader = (id) => findUserGroupCount(id).findComponent(GlSkeletonLoader);
2021-02-22 17:27:13 +05:30
const getCellByLabel = (trIdx, label) => {
return wrapper
2022-08-27 11:52:29 +05:30
.findComponent(GlTable)
2021-02-22 17:27:13 +05:30
.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,
},
});
};
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', () => {
2022-08-27 11:52:29 +05:30
expect(wrapper.findComponent(AdminUserActions).exists()).toBe(true);
2021-03-11 19:13:27 +05:30
});
it.each`
component | label
${AdminUserAvatar} | ${'Name'}
${AdminUserDate} | ${'Created on'}
${AdminUserDate} | ${'Last activity'}
`('renders the component for column $label', ({ component, label }) => {
2022-08-27 11:52:29 +05:30
expect(getCellByLabel(0, label).findComponent(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
});
2023-05-27 22:25:52 +05:30
it('creates an alert message and captures the error', () => {
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalledWith({
2021-06-08 01:23:25 +05:30
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
});