๐Ÿ“ฆ situ2001 / scoped-rem

๐Ÿ“„ options.test.ts ยท 48 lines
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
48import { test, expect } from 'vitest';
import { parseQueryOptions, VARNAME_DEFAULT, VARSELECTOR_DEFAULT } from '../src/index.js';

test('parseQueryOptions', () => {
  expect(parseQueryOptions('')).toBeNull();

  // with rem-scoped
  expect(parseQueryOptions('?rem-scoped')).toEqual({
    varname: VARNAME_DEFAULT,
    varselector: VARSELECTOR_DEFAULT,
  });
  // without rem-scoped
  expect(parseQueryOptions('?varname=my-var&rootval=10vw&varselector=.comp&precision=3')).toBeNull();

  // basic options
  expect(parseQueryOptions('?rem-scoped&varname=my-var')).toEqual({
    varname: 'my-var',
    varselector: VARSELECTOR_DEFAULT,
  });
  expect(parseQueryOptions('?rem-scoped&rootval=16px')).toEqual({
    rootval: '16px',
    varname: VARNAME_DEFAULT,
    varselector: VARSELECTOR_DEFAULT,
  });
  expect(parseQueryOptions('?rem-scoped&varselector=.my-class')).toEqual({
    varselector: '.my-class',
    varname: VARNAME_DEFAULT,
  });
  expect(parseQueryOptions('?rem-scoped&precision=2')).toEqual({
    precision: 2,
    varname: VARNAME_DEFAULT,
    varselector: VARSELECTOR_DEFAULT,
  });

  expect(parseQueryOptions('?rem-scoped&varname=my-var&rootval=10vw&varselector=.comp&precision=3')).toEqual({
    varname: 'my-var',
    rootval: '10vw',
    varselector: '.comp',
    precision: 3,
  });

  // invalid precision
  expect(() => parseQueryOptions('?rem-scoped&precision=-1')).toThrow();
  expect(() => parseQueryOptions('?rem-scoped&precision=101')).toThrow();
  expect(() => parseQueryOptions('?rem-scoped&precision=2.5')).toThrow();
  expect(() => parseQueryOptions('?rem-scoped&precision=abc')).toThrow();
});