summaryrefslogtreecommitdiff
path: root/spec/frontend/admin/abuse_reports/components/app_spec.js
blob: 41728baaf33e087bd0f8d899d1fbe0b524a45df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { shallowMount } from '@vue/test-utils';
import { GlEmptyState, GlPagination } from '@gitlab/ui';
import { queryToObject, objectToQuery } from '~/lib/utils/url_utility';
import setWindowLocation from 'helpers/set_window_location_helper';
import AbuseReportsApp from '~/admin/abuse_reports/components/app.vue';
import AbuseReportsFilteredSearchBar from '~/admin/abuse_reports/components/abuse_reports_filtered_search_bar.vue';
import AbuseReportRow from '~/admin/abuse_reports/components/abuse_report_row.vue';
import { mockAbuseReports } from '../mock_data';

describe('AbuseReportsApp', () => {
  let wrapper;

  const findFilteredSearchBar = () => wrapper.findComponent(AbuseReportsFilteredSearchBar);
  const findEmptyState = () => wrapper.findComponent(GlEmptyState);
  const findAbuseReportRows = () => wrapper.findAllComponents(AbuseReportRow);
  const findPagination = () => wrapper.findComponent(GlPagination);

  const createComponent = (props = {}) => {
    wrapper = shallowMount(AbuseReportsApp, {
      propsData: {
        abuseReports: mockAbuseReports,
        pagination: { currentPage: 1, perPage: 20, totalItems: mockAbuseReports.length },
        ...props,
      },
    });
  };

  it('renders AbuseReportsFilteredSearchBar', () => {
    createComponent();

    expect(findFilteredSearchBar().exists()).toBe(true);
  });

  it('renders one AbuseReportRow for each abuse report', () => {
    createComponent();

    expect(findEmptyState().exists()).toBe(false);
    expect(findAbuseReportRows().length).toBe(mockAbuseReports.length);
  });

  it('renders empty state when there are no reports', () => {
    createComponent({
      abuseReports: [],
      pagination: { currentPage: 1, perPage: 20, totalItems: 0 },
    });

    expect(findEmptyState().exists()).toBe(true);
  });

  describe('pagination', () => {
    const pagination = {
      currentPage: 1,
      perPage: 1,
      totalItems: mockAbuseReports.length,
    };

    it('renders GlPagination with the correct props when needed', () => {
      createComponent({ pagination });

      expect(findPagination().exists()).toBe(true);
      expect(findPagination().props()).toMatchObject({
        value: pagination.currentPage,
        perPage: pagination.perPage,
        totalItems: pagination.totalItems,
        prevText: 'Prev',
        nextText: 'Next',
        labelNextPage: 'Go to next page',
        labelPrevPage: 'Go to previous page',
        align: 'center',
      });
    });

    it('does not render GlPagination when not needed', () => {
      createComponent({ pagination: { currentPage: 1, perPage: 2, totalItems: 2 } });

      expect(findPagination().exists()).toBe(false);
    });

    describe('linkGen prop', () => {
      const existingQuery = {
        user: 'mr_okay',
        status: 'closed',
      };
      const expectedGeneratedQuery = {
        ...existingQuery,
        page: '2',
      };

      beforeEach(() => {
        setWindowLocation(`https://localhost?${objectToQuery(existingQuery)}`);
      });

      it('generates the correct page URL', () => {
        createComponent({ pagination });

        const linkGen = findPagination().props('linkGen');
        const generatedUrl = linkGen(expectedGeneratedQuery.page);
        const [, generatedQuery] = generatedUrl.split('?');

        expect(queryToObject(generatedQuery)).toMatchObject(expectedGeneratedQuery);
      });
    });
  });
});