Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial v2 refactor #131

Draft
wants to merge 4 commits into
base: ts_refactor
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 0 additions & 13 deletions .babelrc

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ jsonata-es5.min.js
node_modules/
npm-debug.log
.idea/
es2015/
es5/
dist/
104 changes: 63 additions & 41 deletions test/async-function.js → __tests__/async-function.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
"use strict";

var jsonata = require('../jsonata');
var jsonata = require('../src').jsonata;
var request = require('request');
//var assert = require('assert');
var chai = require("chai");
var expect = chai.expect;
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);

var jsonataPromise = function(expr, data, bindings) {
var jsonataPromise = function(expr, data?, bindings?) {
return new Promise(function(resolve, reject) {
expr.evaluate(data, bindings, function(error, response) {
if(error) reject(error);
Expand All @@ -31,10 +26,12 @@ var httpget = function(url) {

describe('Invoke JSONata with callback', function() {
describe('Make HTTP request', function() {
it('should return promise to results', function() {
it('should return promise to results', async () => {
var expr = jsonata('$httpget("https://api.npmjs.org/downloads/range/2016-09-01:2017-03-31/jsonata").downloads{ $substring(day, 0, 7): $sum(downloads) }');
expr.assign('httpget', httpget);
return expect(jsonataPromise(expr)).to.eventually.deep.equal({
let result = await jsonataPromise(expr);

expect(result).toEqual({
'2016-09': 205,
'2016-10': 1266,
'2016-11': 2783,
Expand All @@ -49,45 +46,57 @@ describe('Invoke JSONata with callback', function() {

describe('Invoke JSONata with callback - errors', function() {
describe('type error', function() {
it('should throw', function() {
it('should throw', async () => {
var expr = jsonata('5 + $httpget("htttttps://api.npmjs.org/downloads/range/2016-09-01:2017-03-31/jsonata")');
expr.assign('httpget', httpget);
return expect(jsonataPromise(expr)).to.be.rejected;
// .to.deep.contain({position: 7, code: 'T0410', token: 'count', index: 2});;

try {
await jsonataPromise(expr);
expect(undefined).toBeDefined();
} catch(e) {
// Expected outcome
}
});

});

describe('Make HTTP request with dodgy url', function() {
it('should throw', function() {
it('should throw', async () => {
var expr = jsonata('$httpget("htttttps://api.npmjs.org/downloads/range/2016-09-01:2017-03-31/jsonata").downloads{ $substring(day, 0, 7): $sum(downloads) }');
expr.assign('httpget', httpget);
return expect(jsonataPromise(expr)).to.be.rejected;
// .to.deep.contain({position: 7, code: 'T0410', token: 'count', index: 2});;
try {
await jsonataPromise(expr);
expect(undefined).toBeDefined();
} catch(e) {
// Expected outcome
}
});
});
});

describe('Invoke JSONata with callback - return values', function() {
it('should handle an undefined value', function() {
it('should handle an undefined value', async () => {
var data = { value: undefined };
var expr = jsonata('value');
return expect(jsonataPromise(expr, data)).to.eventually.equal(undefined);
let result = await jsonataPromise(expr, data);
expect(result).toEqual(undefined);
});
it('should handle a null value', function() {
it('should handle a null value', async() => {
var data = { value: null };
var expr = jsonata('value');
return expect(jsonataPromise(expr, data)).to.eventually.equal(null);
let result = await jsonataPromise(expr, data);
return expect(result).toEqual(null);
});
it('should handle a value', function() {
it('should handle a value', async() => {
var data = { value: 'hello' };
var expr = jsonata('value');
return expect(jsonataPromise(expr, data)).to.eventually.equal('hello');
let result = await jsonataPromise(expr, data);
return expect(result).toEqual('hello');
});
it('should handle a promise', function() {
it('should handle a promise', async() => {
var data = { value: Promise.resolve('hello') };
var expr = jsonata('value');
return expect(jsonataPromise(expr, data)).to.eventually.equal('hello');
let result = await jsonataPromise(expr, data);
return expect(result).toEqual('hello');
});
});

Expand All @@ -111,52 +120,65 @@ describe('Handle chained functions that end in promises', function() {
counter: counter
};

it('basic function that returns a thenable', function() {
it('basic function that returns a thenable', async() => {
var data = {};
var expr = jsonata('$counter(5)');
return expect(jsonataPromise(expr, data, bindings)).to.eventually.equal(5);
let result = await jsonataPromise(expr, data, bindings);
expect(result).toEqual(5);
});

it('basic function that returns a thenable, but invokes another function', function() {
it('basic function that returns a thenable, but invokes another function', async() => {
var data = {};
var expr = jsonata('$counter(0).inc()');
return expect(jsonataPromise(expr, data, bindings)).to.eventually.equal(1);
let result = await jsonataPromise(expr, data, bindings);
return expect(result).toEqual(1);
});

it('basic function that returns a thenable, but invokes another function several times', function() {
it('basic function that returns a thenable, but invokes another function several times', async() => {
var data = {};
var expr = jsonata('$counter(0).inc().inc().inc().inc()');
return expect(jsonataPromise(expr, data, bindings)).to.eventually.equal(4);
let result = await jsonataPromise(expr, data, bindings);
return expect(result).toEqual(4);
});

it('basic function that returns a thenable and part of a numeric expression', function() {
it('basic function that returns a thenable and part of a numeric expression', async() => {
var data = {};
var expr = jsonata('$counter(3) + 5');
return expect(jsonataPromise(expr, data, bindings)).to.eventually.equal(8);
let result = await jsonataPromise(expr, data, bindings);
return expect(result).toEqual(8);
});

it('basic function that returns a thenable, but invokes another function several times and part of a numeric expression', function() {
it('basic function that returns a thenable, but invokes another function several times and part of a numeric expression', async() => {
var data = {};
var expr = jsonata('$counter(0).inc().inc().inc().inc() + 3');
return expect(jsonataPromise(expr, data, bindings)).to.eventually.equal(7);
let result = await jsonataPromise(expr, data, bindings);

return expect(result).toEqual(7);
});

it('basic function that returns a thenable, but invokes another function - nested', function() {
it('basic function that returns a thenable, but invokes another function - nested', async() => {
var data = {};
var expr = jsonata('$counter($counter(3).inc().inc()).inc()');
return expect(jsonataPromise(expr, data, bindings)).to.eventually.equal(6);
let result = await jsonataPromise(expr, data, bindings);
return expect(result).toEqual(6);
});

it('basic function that returns a thenable, then invokes a built-in function', function() {
it('basic function that returns a thenable, then invokes a built-in function', async() => {
var data = {};
var expr = jsonata('$counter(3).inc().$string()');
return expect(jsonataPromise(expr, data, bindings)).to.eventually.equal('4');
let result = await jsonataPromise(expr, data, bindings);
return expect(result).toEqual('4');
});

it('basic function that returns a thenable, but invokes a non-existent function', function() {
it('basic function that returns a thenable, but invokes a non-existent function', async() => {
var data = {};
var expr = jsonata('$counter(2).inc().foo()');
return expect(jsonataPromise(expr, data, bindings)).to.be.rejected;
});

try {
await jsonataPromise(expr, data, bindings);
expect(undefined).toBeDefined();
} catch(e) {
// Expected outcome
}
});
});
Loading