summaryrefslogtreecommitdiff
path: root/spec/frontend/notes/utils/get_notes_filter_data_spec.js
blob: c3a8d3bc61902e1e20ec98609e886d99eee55e53 (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
import { getNotesFilterData } from '~/notes/utils/get_notes_filter_data';
import { notesFilters } from '../mock_data';

// what: This is the format we expect the element attribute to be in
// why: For readability, we make this clear by hardcoding the indecise instead of using `reduce`.
const TEST_NOTES_FILTERS_ATTR = {
  [notesFilters[0].title]: notesFilters[0].value,
  [notesFilters[1].title]: notesFilters[1].value,
  [notesFilters[2].title]: notesFilters[2].value,
};

describe('~/notes/utils/get_notes_filter_data', () => {
  it.each([
    {
      desc: 'empty',
      attributes: {},
      expectation: {
        notesFilters: [],
        notesFilterValue: undefined,
      },
    },
    {
      desc: 'valid attributes',
      attributes: {
        'data-notes-filters': JSON.stringify(TEST_NOTES_FILTERS_ATTR),
        'data-notes-filter-value': '1',
      },
      expectation: {
        notesFilters,
        notesFilterValue: 1,
      },
    },
  ])('with $desc, parses data from element attributes', ({ attributes, expectation }) => {
    const el = document.createElement('div');

    Object.entries(attributes).forEach(([key, value]) => {
      el.setAttribute(key, value);
    });

    const actual = getNotesFilterData(el);

    expect(actual).toStrictEqual(expectation);
  });
});