UNPKG

2.27 kBJavaScriptView Raw
1const {compile, and, or, root, arg0, setter, chain, abstract, implement} = require('../../index');
2const {
3 describeCompilers,
4 evalOrLoad,
5 currentValues,
6 funcLibrary,
7 expectTapFunctionToHaveBeenCalled,
8 rand
9 } = require('../test-utils');
10 const _ = require('lodash');
11
12const compiler = 'simple';
13
14describe('test the usage of abstracts', () => {
15 it('should be able to create a abstract and implement it later', () => {
16 const todos = abstract('todos');
17 const todoTitles = todos.map(todoItem => todoItem.get('text'));
18 const allDone = todos.any(todoItem => todoItem.get('done').not()).not()
19 implement(todos, root.get('todos'));
20 const model = {allDone, todoTitles, set: setter('todos', arg0)}
21 const optCode = evalOrLoad(compile(model, {compiler}));
22 const initialState = {todos: [{text: 'first', done: false}, {text: 'second', done: true}]}
23 const inst = optCode(initialState, funcLibrary);
24 expect(inst.todoTitles).toEqual(['first', 'second']);
25 expect(inst.allDone).toEqual(false);
26 });
27 it('should throw if abstract is used in expression trying to implement abstract', () => {
28 const todos = abstract('todos');
29 const todoTitles = todos.map(todoItem => todoItem.get('title'));
30 const allDone = todos.any(todoItem => todoItem.get('done').not()).not()
31 expect(() => {
32 implement(todos, todos.get(0))
33 }).toThrowError()
34 });
35 it('should be able to create a abstract and implement it later even if the implementation is a primitive', () => {
36 const value = abstract('value');
37 const items = root.map(item => item.plus(value));
38 implement(value, chain(3));
39 const model = {items, set: setter(arg0)}
40 const optCode = evalOrLoad(compile(model, {compiler, debug: true}));
41 const initialState = [1, 2, 3]
42 const inst = optCode(initialState, funcLibrary);
43 expect(inst.items).toEqual([4, 5, 6]);
44 });
45 it('should throw if implement an abstract using itself', () => {
46 const A = abstract('first abstract');
47 const B = abstract('second abstract');
48 implement(A, B)
49 expect(() => {
50 implement(B, A)
51 }).toThrowError()
52 });
53})