aboutsummaryrefslogtreecommitdiff
path: root/tests/eval.spec.ts
blob: 1d8b16132bbd32169c653b198384d7b03a93186c (plain) (blame)
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
105
106
107
108
109
110
111
import { EvalActions, evalExpr } from '../src/eval'
import { Expr } from '../src/parser'

describe('eval', () => {
  const deps: EvalActions = {
    addClass: jest.fn(),
    removeClass: jest.fn(),
    delay: jest.fn(),
    jsEval: jest.fn(),
    loadCssx: jest.fn(),
    getVariable: jest.fn(),
    updateVariable: jest.fn(),
    setAttribute: jest.fn(),
  }

  it('should add classes', async () => {
    await evalExpr(
      Expr.Call({
        name: 'add-class',
        args: [Expr.Identifier('element-id'), Expr.LiteralString('class-name')],
      }),
      deps,
    )

    expect(deps.addClass).toHaveBeenCalledTimes(1)
    expect(deps.addClass).toHaveBeenCalledWith('element-id', 'class-name')
  })

  it('should allow conditionals classes', async () => {
    expect(
      await evalExpr(
        Expr.Call({
          name: 'if',
          args: [
            Expr.Identifier('true'),
            Expr.Identifier('yes'),
            Expr.Identifier('no'),
          ],
        }),
        deps,
      ),
    ).toBe('yes')

    expect(
      await evalExpr(
        Expr.Call({
          name: 'if',
          args: [
            Expr.Identifier('false'),
            Expr.Identifier('yes'),
            Expr.Identifier('no'),
          ],
        }),
        deps,
      ),
    ).toBe('no')

    expect(
      await evalExpr(
        Expr.Call({
          name: 'if',
          args: [
            Expr.Identifier('0'),
            Expr.Identifier('yes'),
            Expr.Identifier('no'),
          ],
        }),
        deps,
      ),
    ).toBe('no')
  })

  it('should remove classes', async () => {
    await evalExpr(
      Expr.Call({
        name: 'remove-class',
        args: [Expr.Identifier('element-id'), Expr.LiteralString('class-name')],
      }),
      deps,
    )

    expect(deps.removeClass).toHaveBeenCalledTimes(1)
    expect(deps.removeClass).toHaveBeenCalledWith('element-id', 'class-name')
  })

  it('should add a delay', async () => {
    await evalExpr(
      Expr.Call({
        name: 'delay',
        args: [Expr.LiteralString('200')],
      }),
      deps,
    )

    expect(deps.delay).toHaveBeenCalledTimes(1)
    expect(deps.delay).toHaveBeenCalledWith(200)
  })

  it('should get variable', async () => {
    await evalExpr(
      Expr.Call({
        name: 'var',
        args: [Expr.LiteralString('--my-var'), Expr.LiteralString('def value')],
      }),
      deps,
    )

    expect(deps.getVariable).toHaveBeenCalledTimes(1)
    expect(deps.getVariable).toHaveBeenCalledWith('--my-var')
  })
})