runner_search_utils_spec.js 9.48 KB
Newer Older
Miguel Rincon's avatar
Miguel Rincon committed
1
import { RUNNER_PAGE_SIZE } from '~/runner/constants';
2
import {
3
  searchValidator,
4
  updateOutdatedUrl,
5 6 7
  fromUrlQueryToSearch,
  fromSearchToUrl,
  fromSearchToVariables,
8
} from '~/runner/runner_search_utils';
9 10 11 12 13 14

describe('search_params.js', () => {
  const examples = [
    {
      name: 'a default query',
      urlQuery: '',
15
      search: { runnerType: null, filters: [], pagination: { page: 1 }, sort: 'CREATED_DESC' },
Miguel Rincon's avatar
Miguel Rincon committed
16
      graphqlVariables: { sort: 'CREATED_DESC', first: RUNNER_PAGE_SIZE },
17 18 19 20 21
    },
    {
      name: 'a single status',
      urlQuery: '?status[]=ACTIVE',
      search: {
22
        runnerType: null,
23
        filters: [{ type: 'status', value: { data: 'ACTIVE', operator: '=' } }],
Miguel Rincon's avatar
Miguel Rincon committed
24
        pagination: { page: 1 },
25 26
        sort: 'CREATED_DESC',
      },
Miguel Rincon's avatar
Miguel Rincon committed
27
      graphqlVariables: { status: 'ACTIVE', sort: 'CREATED_DESC', first: RUNNER_PAGE_SIZE },
28
    },
29 30 31 32
    {
      name: 'a single term text search',
      urlQuery: '?search=something',
      search: {
33
        runnerType: null,
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        filters: [
          {
            type: 'filtered-search-term',
            value: { data: 'something' },
          },
        ],
        pagination: { page: 1 },
        sort: 'CREATED_DESC',
      },
      graphqlVariables: { search: 'something', sort: 'CREATED_DESC', first: RUNNER_PAGE_SIZE },
    },
    {
      name: 'a two terms text search',
      urlQuery: '?search=something+else',
      search: {
49
        runnerType: null,
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        filters: [
          {
            type: 'filtered-search-term',
            value: { data: 'something' },
          },
          {
            type: 'filtered-search-term',
            value: { data: 'else' },
          },
        ],
        pagination: { page: 1 },
        sort: 'CREATED_DESC',
      },
      graphqlVariables: { search: 'something else', sort: 'CREATED_DESC', first: RUNNER_PAGE_SIZE },
    },
65 66 67 68
    {
      name: 'single instance type',
      urlQuery: '?runner_type[]=INSTANCE_TYPE',
      search: {
69 70
        runnerType: 'INSTANCE_TYPE',
        filters: [],
Miguel Rincon's avatar
Miguel Rincon committed
71
        pagination: { page: 1 },
72 73
        sort: 'CREATED_DESC',
      },
Miguel Rincon's avatar
Miguel Rincon committed
74
      graphqlVariables: { type: 'INSTANCE_TYPE', sort: 'CREATED_DESC', first: RUNNER_PAGE_SIZE },
75 76 77 78 79
    },
    {
      name: 'multiple runner status',
      urlQuery: '?status[]=ACTIVE&status[]=PAUSED',
      search: {
80
        runnerType: null,
81 82 83 84
        filters: [
          { type: 'status', value: { data: 'ACTIVE', operator: '=' } },
          { type: 'status', value: { data: 'PAUSED', operator: '=' } },
        ],
Miguel Rincon's avatar
Miguel Rincon committed
85
        pagination: { page: 1 },
86 87
        sort: 'CREATED_DESC',
      },
Miguel Rincon's avatar
Miguel Rincon committed
88
      graphqlVariables: { status: 'ACTIVE', sort: 'CREATED_DESC', first: RUNNER_PAGE_SIZE },
89 90 91 92 93
    },
    {
      name: 'multiple status, a single instance type and a non default sort',
      urlQuery: '?status[]=ACTIVE&runner_type[]=INSTANCE_TYPE&sort=CREATED_ASC',
      search: {
94 95
        runnerType: 'INSTANCE_TYPE',
        filters: [{ type: 'status', value: { data: 'ACTIVE', operator: '=' } }],
Miguel Rincon's avatar
Miguel Rincon committed
96
        pagination: { page: 1 },
97 98
        sort: 'CREATED_ASC',
      },
Miguel Rincon's avatar
Miguel Rincon committed
99 100 101 102 103 104 105
      graphqlVariables: {
        status: 'ACTIVE',
        type: 'INSTANCE_TYPE',
        sort: 'CREATED_ASC',
        first: RUNNER_PAGE_SIZE,
      },
    },
106 107 108 109
    {
      name: 'a tag',
      urlQuery: '?tag[]=tag-1',
      search: {
110
        runnerType: null,
111 112 113 114 115 116 117 118 119 120 121 122 123 124
        filters: [{ type: 'tag', value: { data: 'tag-1', operator: '=' } }],
        pagination: { page: 1 },
        sort: 'CREATED_DESC',
      },
      graphqlVariables: {
        tagList: ['tag-1'],
        first: 20,
        sort: 'CREATED_DESC',
      },
    },
    {
      name: 'two tags',
      urlQuery: '?tag[]=tag-1&tag[]=tag-2',
      search: {
125
        runnerType: null,
126 127 128 129 130 131 132 133 134 135 136 137 138
        filters: [
          { type: 'tag', value: { data: 'tag-1', operator: '=' } },
          { type: 'tag', value: { data: 'tag-2', operator: '=' } },
        ],
        pagination: { page: 1 },
        sort: 'CREATED_DESC',
      },
      graphqlVariables: {
        tagList: ['tag-1', 'tag-2'],
        first: 20,
        sort: 'CREATED_DESC',
      },
    },
Miguel Rincon's avatar
Miguel Rincon committed
139 140 141
    {
      name: 'the next page',
      urlQuery: '?page=2&after=AFTER_CURSOR',
142 143 144 145 146 147
      search: {
        runnerType: null,
        filters: [],
        pagination: { page: 2, after: 'AFTER_CURSOR' },
        sort: 'CREATED_DESC',
      },
Miguel Rincon's avatar
Miguel Rincon committed
148 149 150 151 152 153
      graphqlVariables: { sort: 'CREATED_DESC', after: 'AFTER_CURSOR', first: RUNNER_PAGE_SIZE },
    },
    {
      name: 'the previous page',
      urlQuery: '?page=2&before=BEFORE_CURSOR',
      search: {
154
        runnerType: null,
Miguel Rincon's avatar
Miguel Rincon committed
155 156 157 158 159 160 161
        filters: [],
        pagination: { page: 2, before: 'BEFORE_CURSOR' },
        sort: 'CREATED_DESC',
      },
      graphqlVariables: { sort: 'CREATED_DESC', before: 'BEFORE_CURSOR', last: RUNNER_PAGE_SIZE },
    },
    {
162
      name: 'the next page filtered by a status, an instance type, tags and a non default sort',
Miguel Rincon's avatar
Miguel Rincon committed
163
      urlQuery:
164
        '?status[]=ACTIVE&runner_type[]=INSTANCE_TYPE&tag[]=tag-1&tag[]=tag-2&sort=CREATED_ASC&page=2&after=AFTER_CURSOR',
Miguel Rincon's avatar
Miguel Rincon committed
165
      search: {
166
        runnerType: 'INSTANCE_TYPE',
Miguel Rincon's avatar
Miguel Rincon committed
167 168
        filters: [
          { type: 'status', value: { data: 'ACTIVE', operator: '=' } },
169 170
          { type: 'tag', value: { data: 'tag-1', operator: '=' } },
          { type: 'tag', value: { data: 'tag-2', operator: '=' } },
Miguel Rincon's avatar
Miguel Rincon committed
171 172 173 174 175 176 177
        ],
        pagination: { page: 2, after: 'AFTER_CURSOR' },
        sort: 'CREATED_ASC',
      },
      graphqlVariables: {
        status: 'ACTIVE',
        type: 'INSTANCE_TYPE',
178
        tagList: ['tag-1', 'tag-2'],
Miguel Rincon's avatar
Miguel Rincon committed
179 180 181 182
        sort: 'CREATED_ASC',
        after: 'AFTER_CURSOR',
        first: RUNNER_PAGE_SIZE,
      },
183 184 185
    },
  ];

186 187 188 189 190 191 192 193
  describe('searchValidator', () => {
    examples.forEach(({ name, search }) => {
      it(`Validates ${name} as a search object`, () => {
        expect(searchValidator(search)).toBe(true);
      });
    });
  });

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  describe('updateOutdatedUrl', () => {
    it('returns null for urls that do not need updating', () => {
      expect(updateOutdatedUrl('http://test.host/')).toBe(null);
      expect(updateOutdatedUrl('http://test.host/?a=b')).toBe(null);
    });

    it('returns updated url for updating NOT_CONNECTED to NEVER_CONTACTED', () => {
      expect(updateOutdatedUrl('http://test.host/admin/runners?status[]=NOT_CONNECTED')).toBe(
        'http://test.host/admin/runners?status[]=NEVER_CONTACTED',
      );

      expect(updateOutdatedUrl('http://test.host/admin/runners?status[]=NOT_CONNECTED&a=b')).toBe(
        'http://test.host/admin/runners?status[]=NEVER_CONTACTED&a=b',
      );
    });
  });

211 212 213 214 215 216
  describe('fromUrlQueryToSearch', () => {
    examples.forEach(({ name, urlQuery, search }) => {
      it(`Converts ${name} to a search object`, () => {
        expect(fromUrlQueryToSearch(urlQuery)).toEqual(search);
      });
    });
Miguel Rincon's avatar
Miguel Rincon committed
217

218 219 220 221 222 223 224
    it('When search params appear as array, they are concatenated', () => {
      expect(fromUrlQueryToSearch('?search[]=my&search[]=text').filters).toEqual([
        { type: 'filtered-search-term', value: { data: 'my' } },
        { type: 'filtered-search-term', value: { data: 'text' } },
      ]);
    });

Miguel Rincon's avatar
Miguel Rincon committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    it('When a page cannot be parsed as a number, it defaults to `1`', () => {
      expect(fromUrlQueryToSearch('?page=NONSENSE&after=AFTER_CURSOR').pagination).toEqual({
        page: 1,
      });
    });

    it('When a page is less than 1, it defaults to `1`', () => {
      expect(fromUrlQueryToSearch('?page=0&after=AFTER_CURSOR').pagination).toEqual({
        page: 1,
      });
    });

    it('When a page with no cursor is given, it defaults to `1`', () => {
      expect(fromUrlQueryToSearch('?page=2').pagination).toEqual({
        page: 1,
      });
    });
242 243 244 245 246 247 248 249 250
  });

  describe('fromSearchToUrl', () => {
    examples.forEach(({ name, urlQuery, search }) => {
      it(`Converts ${name} to a url`, () => {
        expect(fromSearchToUrl(search)).toEqual(`http://test.host/${urlQuery}`);
      });
    });

251 252 253 254 255
    it.each([
      'http://test.host/?status[]=ACTIVE',
      'http://test.host/?runner_type[]=INSTANCE_TYPE',
      'http://test.host/?search=my_text',
    ])('When a filter is removed, it is removed from the URL', (initalUrl) => {
256 257 258
      const search = { filters: [], sort: 'CREATED_DESC' };
      const expectedUrl = `http://test.host/`;

259
      expect(fromSearchToUrl(search, initalUrl)).toEqual(expectedUrl);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    });

    it('When unrelated search parameter is present, it does not get removed', () => {
      const initialUrl = `http://test.host/?unrelated=UNRELATED&status[]=ACTIVE`;
      const search = { filters: [], sort: 'CREATED_DESC' };
      const expectedUrl = `http://test.host/?unrelated=UNRELATED`;

      expect(fromSearchToUrl(search, initialUrl)).toEqual(expectedUrl);
    });
  });

  describe('fromSearchToVariables', () => {
    examples.forEach(({ name, graphqlVariables, search }) => {
      it(`Converts ${name} to a GraphQL query variables object`, () => {
        expect(fromSearchToVariables(search)).toEqual(graphqlVariables);
      });
    });
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

    it('When a search param is empty, it gets removed', () => {
      expect(
        fromSearchToVariables({
          filters: [
            {
              type: 'filtered-search-term',
              value: { data: '' },
            },
          ],
        }),
      ).toMatchObject({
        search: '',
      });

      expect(
        fromSearchToVariables({
          filters: [
            {
              type: 'filtered-search-term',
              value: { data: 'something' },
            },
            {
              type: 'filtered-search-term',
              value: { data: '' },
            },
          ],
        }),
      ).toMatchObject({
        search: 'something',
      });
    });
309 310
  });
});