UNPKG

1.08 kBJavaScriptView Raw
1const {root, compile} = require('../../index');
2const {describeCompilers, evalOrLoad} = require('../test-utils');
3
4describe('switch', () => {
5 describeCompilers(['simple', 'optimizing', 'bytecode'], compiler => {
6 it('should return the result of the matching case tuple', () => {
7 const model = {
8 result: root.get('a').switch([
9 [1, 'One'],
10 [2, 'Two'],
11 [3, 'Three']
12 ])
13 };
14
15 const optModel = evalOrLoad(compile(model, {compiler}));
16 const initialData = {
17 a: 2
18 };
19
20 const inst = optModel(initialData);
21 expect(inst.result).toEqual('Two');
22 })
23
24 it('should return the default result if no case matches', () => {
25 const model = {
26 result: root.get('a').switch([
27 [1, 'One'],
28 [2, 'Two'],
29 [3, 'Three']
30 ], 'Oops')
31 };
32
33 const optModel = evalOrLoad(compile(model, {compiler}));
34 const initialData = {
35 a: 4
36 };
37
38 const inst = optModel(initialData);
39 expect(inst.result).toEqual('Oops');
40 })
41 })
42})