-
Notifications
You must be signed in to change notification settings - Fork 0
/
Executor.test.js
49 lines (43 loc) · 1.33 KB
/
Executor.test.js
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
/* globals describe, it, expect */
const Executor = require('./Executor')
describe('Executor', () => {
it('can be created', () => {
const d = new Executor()
expect(typeof d).toEqual('object')
})
it('exposes minimal public API', () => {
const d = new Executor()
expect(Object.keys(d)).toEqual([])
expect(Object.getOwnPropertyNames(d)).toEqual([])
expect(Object.getOwnPropertyNames(Object.getPrototypeOf(d))).toEqual([
'constructor',
'exec'
])
})
it('can execute javascript code', async () => {
const d = new Executor()
const result = d.exec('Date.now()', {})
expect(result).toBeInstanceOf(Promise)
expect(typeof await result).toEqual('number')
})
it('can modify state using global variables', async () => {
const state = {}
const d = new Executor()
expect(await d.exec('x = 10', state)).toEqual(10)
expect(state).toEqual({ x: 10 })
})
it('changes to state persists', async () => {
const state = {}
const d = new Executor()
await d.exec('x = 10', state)
await d.exec('y = 20', state)
expect(state).toEqual({ x: 10, y: 20 })
})
it('supports `cd`', async () => {
const state = {}
const d = new Executor()
await d.exec('test = {a: 1}', state)
await d.exec('cd("test")', state)
expect(await d.exec('a', state)).toEqual(1)
})
})