|
| 1 | +import { stripIndent } from 'common-tags' |
| 2 | + |
| 3 | +import { mockContext } from '../../mocks/context' |
| 4 | +import { runInContext } from '../index' |
| 5 | + |
| 6 | +test('Empty code returns undefined', () => { |
| 7 | + const code = '' |
| 8 | + const context = mockContext() |
| 9 | + const promise = runInContext(code, context) |
| 10 | + return promise.then(obj => { |
| 11 | + expect(obj).toMatchSnapshot() |
| 12 | + expect(obj.status).toBe('finished') |
| 13 | + expect(obj.value).toBe(undefined) |
| 14 | + }) |
| 15 | +}) |
| 16 | + |
| 17 | +test('Single string self-evaluates to itself', () => { |
| 18 | + const code = "'42';" |
| 19 | + const context = mockContext() |
| 20 | + const promise = runInContext(code, context) |
| 21 | + return promise.then(obj => { |
| 22 | + expect(obj).toMatchSnapshot() |
| 23 | + expect(obj.status).toBe('finished') |
| 24 | + expect(obj.value).toBe('42') |
| 25 | + }) |
| 26 | +}) |
| 27 | + |
| 28 | +test('Single number self-evaluates to itself', () => { |
| 29 | + const code = '42;' |
| 30 | + const context = mockContext() |
| 31 | + const promise = runInContext(code, context) |
| 32 | + return promise.then(obj => { |
| 33 | + expect(obj).toMatchSnapshot() |
| 34 | + expect(obj.status).toBe('finished') |
| 35 | + expect(obj.value).toBe(42) |
| 36 | + }) |
| 37 | +}) |
| 38 | + |
| 39 | +test('Single boolean self-evaluates to itself', () => { |
| 40 | + const code = 'true;' |
| 41 | + const context = mockContext() |
| 42 | + const promise = runInContext(code, context) |
| 43 | + return promise.then(obj => { |
| 44 | + expect(obj).toMatchSnapshot() |
| 45 | + expect(obj.status).toBe('finished') |
| 46 | + expect(obj.value).toBe(true) |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +test('Arrow function definition returns itself', () => { |
| 51 | + const code = '() => 42;' |
| 52 | + const context = mockContext() |
| 53 | + const promise = runInContext(code, context) |
| 54 | + return promise.then(obj => { |
| 55 | + expect(obj).toMatchSnapshot() |
| 56 | + expect(obj.status).toBe('finished') |
| 57 | + expect(obj.value).toMatchSnapshot() |
| 58 | + }) |
| 59 | +}) |
| 60 | + |
| 61 | +test('Factorial arrow function', () => { |
| 62 | + const code = stripIndent` |
| 63 | + const fac = (i) => i === 1 ? 1 : i * fac(i-1); |
| 64 | + fac(5); |
| 65 | + ` |
| 66 | + const context = mockContext() |
| 67 | + const promise = runInContext(code, context) |
| 68 | + return promise.then(obj => { |
| 69 | + expect(obj).toMatchSnapshot() |
| 70 | + expect(obj.status).toBe('finished') |
| 71 | + expect(obj.value).toBe(120) |
| 72 | + }) |
| 73 | +}) |
0 commit comments