debian-mirror-gitlab/spec/frontend/repository/components/tree_content_spec.js

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

202 lines
6.2 KiB
JavaScript
Raw Normal View History

2019-12-26 22:10:19 +05:30
import { shallowMount } from '@vue/test-utils';
2023-07-09 08:55:56 +05:30
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
2019-12-26 22:10:19 +05:30
import FilePreview from '~/repository/components/preview/index.vue';
2020-11-24 15:15:51 +05:30
import FileTable from '~/repository/components/table/index.vue';
2022-01-26 12:08:38 +05:30
import TreeContent from 'jh_else_ce/repository/components/tree_content.vue';
2023-07-09 08:55:56 +05:30
import { TREE_PAGE_LIMIT, i18n } from '~/repository/constants';
2021-11-18 22:05:49 +05:30
import { loadCommits, isRequested, resetRequestedCommits } from '~/repository/commits_service';
2023-07-09 08:55:56 +05:30
import createApolloProvider from 'helpers/mock_apollo_helper';
2023-03-17 16:20:25 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2023-07-09 08:55:56 +05:30
import paginatedTreeQuery from 'shared_queries/repository/paginated_tree.query.graphql';
import projectPathQuery from '~/repository/queries/project_path.query.graphql';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
2023-07-09 08:55:56 +05:30
import { graphQLErrors, paginatedTreeResponseFactory } from '../mock_data';
2021-11-18 22:05:49 +05:30
jest.mock('~/repository/commits_service', () => ({
loadCommits: jest.fn(() => Promise.resolve()),
isRequested: jest.fn(),
resetRequestedCommits: jest.fn(),
}));
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2019-12-26 22:10:19 +05:30
describe('Repository table component', () => {
2023-07-09 08:55:56 +05:30
Vue.use(VueApollo);
let wrapper;
const paginatedTreeResponseWithMoreThanLimit = jest
.fn()
.mockResolvedValue(paginatedTreeResponseFactory({ numberOfBlobs: TREE_PAGE_LIMIT + 2 }));
const paginatedTreeQueryResponseHandler = jest
.fn()
.mockResolvedValue(paginatedTreeResponseFactory());
const findFileTable = () => wrapper.findComponent(FileTable);
const createComponent = ({
path = '/',
responseHandler = paginatedTreeQueryResponseHandler,
} = {}) => {
const apolloProvider = createApolloProvider([[paginatedTreeQuery, responseHandler]]);
apolloProvider.clients.defaultClient.cache.writeQuery({
query: projectPathQuery,
data: {
projectPath: path,
},
});
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
wrapper = shallowMount(TreeContent, {
apolloProvider,
propsData: {
path,
},
});
};
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
it('renders file preview when the response has README.md', async () => {
const paginatedTreeResponseWithReadMe = jest
.fn()
.mockResolvedValue(paginatedTreeResponseFactory({ numberOfBlobs: 1, blobHasReadme: true }));
createComponent({ responseHandler: paginatedTreeResponseWithReadMe });
2022-04-04 11:22:00 +05:30
await nextTick();
2020-10-24 23:57:45 +05:30
2023-07-09 08:55:56 +05:30
await waitForPromises();
expect(wrapper.findComponent(FilePreview).exists()).toBe(true);
2020-10-24 23:57:45 +05:30
});
2023-07-09 08:55:56 +05:30
it('calls tree response handler and resetRequestedCommits when mounted', async () => {
createComponent();
2020-10-24 23:57:45 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2020-10-24 23:57:45 +05:30
2023-07-09 08:55:56 +05:30
expect(paginatedTreeQueryResponseHandler).toHaveBeenCalled();
2021-11-18 22:05:49 +05:30
expect(resetRequestedCommits).toHaveBeenCalled();
2019-12-26 22:10:19 +05:30
});
describe('normalizeData', () => {
2023-07-09 08:55:56 +05:30
it('normalizes edge nodes', async () => {
createComponent();
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
await nextTick();
await waitForPromises();
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
const [
paginatedTreeNode,
] = paginatedTreeResponseFactory().data.project.repository.paginatedTree.nodes;
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
const {
blobs: { nodes: blobs },
trees: { nodes: trees },
submodules: { nodes: submodules },
} = paginatedTreeNode;
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
expect(findFileTable().props('entries')).toEqual({
blobs,
trees,
submodules,
2019-12-26 22:10:19 +05:30
});
});
2023-07-09 08:55:56 +05:30
});
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
describe('when there is next page', () => {
it('make sure it has the correct props to filetable', async () => {
createComponent({ responseHandler: paginatedTreeResponseWithMoreThanLimit });
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
await nextTick();
await waitForPromises();
2019-12-26 22:10:19 +05:30
2023-07-09 08:55:56 +05:30
expect(findFileTable().props('hasMore')).toBe(true);
2019-12-26 22:10:19 +05:30
});
});
2020-10-24 23:57:45 +05:30
2023-07-09 08:55:56 +05:30
describe('FileTable', () => {
describe('when "showMore" event is emitted', () => {
beforeEach(async () => {
createComponent();
2022-04-04 11:22:00 +05:30
await nextTick();
2023-07-09 08:55:56 +05:30
await waitForPromises();
2020-10-24 23:57:45 +05:30
});
2023-07-09 08:55:56 +05:30
it('changes hasShowMore to false', async () => {
2021-09-30 23:02:18 +05:30
findFileTable().vm.$emit('showMore');
2020-10-24 23:57:45 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2020-10-24 23:57:45 +05:30
2023-07-09 08:55:56 +05:30
expect(findFileTable().props('hasMore')).toBe(false);
2020-10-24 23:57:45 +05:30
});
2023-07-09 08:55:56 +05:30
it('triggers the tree responseHandler', () => {
2021-09-30 23:02:18 +05:30
findFileTable().vm.$emit('showMore');
2020-10-24 23:57:45 +05:30
2023-07-09 08:55:56 +05:30
expect(paginatedTreeQueryResponseHandler).toHaveBeenCalled();
2020-10-24 23:57:45 +05:30
});
});
2023-07-09 08:55:56 +05:30
describe('"hasMore" props is correctly computed with the limit to 1000 per page', () => {
it.each`
totalBlobs | limitReached
${500} | ${false}
${900} | ${false}
${1000} | ${true}
${1002} | ${true}
${2000} | ${true}
`(
'is `$limitReached` when the number of entries is `$totalBlobs`',
async ({ totalBlobs, limitReached }) => {
const paginatedTreeResponseHandler = jest
.fn()
.mockResolvedValue(paginatedTreeResponseFactory({ numberOfBlobs: totalBlobs }));
createComponent({ responseHandler: paginatedTreeResponseHandler });
await nextTick();
await waitForPromises();
expect(findFileTable().props('hasMore')).toBe(limitReached);
},
);
2021-09-30 23:02:18 +05:30
});
2020-10-24 23:57:45 +05:30
});
2021-11-18 22:05:49 +05:30
2022-01-26 12:08:38 +05:30
describe('commit data', () => {
2023-07-09 08:55:56 +05:30
const path = '';
2021-11-18 22:05:49 +05:30
2022-01-26 12:08:38 +05:30
it('loads commit data for both top and bottom batches when row-appear event is emitted', () => {
const rowNumber = 50;
2021-11-18 22:05:49 +05:30
2023-07-09 08:55:56 +05:30
createComponent({ path });
2022-01-26 12:08:38 +05:30
findFileTable().vm.$emit('row-appear', rowNumber);
expect(isRequested).toHaveBeenCalledWith(rowNumber);
expect(loadCommits.mock.calls).toEqual([
['', path, '', rowNumber],
['', path, '', rowNumber - 25],
]);
});
it('loads commit data once if rowNumber is zero', () => {
2023-07-09 08:55:56 +05:30
createComponent({ path });
2022-01-26 12:08:38 +05:30
findFileTable().vm.$emit('row-appear', 0);
expect(loadCommits.mock.calls).toEqual([['', path, '', 0]]);
});
2021-11-18 22:05:49 +05:30
});
2023-03-17 16:20:25 +05:30
describe('error handling', () => {
const gitalyError = { graphQLErrors };
it.each`
error | message
${gitalyError} | ${i18n.gitalyError}
${'Error'} | ${i18n.generalError}
2023-07-09 08:55:56 +05:30
`(
`when the graphql error is "$error" shows the message "$message"`,
async ({ error, message }) => {
createComponent({ path: '/', responseHandler: jest.fn().mockRejectedValue(error) });
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith({ message, captureError: true });
},
);
2023-03-17 16:20:25 +05:30
});
2019-12-26 22:10:19 +05:30
});