Skip to content

Commit

Permalink
Hasura:subscribeToMore + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrspartak committed May 30, 2020
1 parent 80abae4 commit b53a79f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"camelcase": 0,
"block-scoped-var": 0,
"no-sequences": 0,
"no-negated-condition": 0
"no-negated-condition": 0,
"no-async-promise-executor": 0
},
"ignores": [
"docs/**"
Expand Down
26 changes: 26 additions & 0 deletions src/orm/hasura.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,32 @@ class Hasura {
return unsub; // Unsubscribe
}

async subscribeToMore(parameters, callback, settings = {}) {
settings = __.mergeDeep(
{
passFirst: false,
},
this.params.query,
settings,
);

callback(await this.query(parameters, settings));

let firstPassed = settings.passFirst;
const subSettings = __.mergeDeep({}, this.params.subscribe, settings);
return this.subscribe(
parameters,
(response) => {
if (firstPassed === true) {
callback(response);
} else {
firstPassed = true;
}
},
subSettings,
);
}

/*
Settings
flatOne
Expand Down
97 changes: 91 additions & 6 deletions tests/subscription.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config();
const test = require('ava');
const {Hasura} = require('../src');
const __ = require('../src/utils/helpers');

test.before(async (t) => {
const orm = new Hasura({
Expand Down Expand Up @@ -102,6 +103,7 @@ test.serial('test sub', async (t) => {
});
t.is(err, null);

__.sleep(1000);
const id = response[0].id;

let responseCount = 0;
Expand All @@ -127,7 +129,7 @@ test.serial('test sub', async (t) => {
}
}, 500);

const unsub = orm.subscribe(
orm.subscribe(
{
_om_test: {
where: {
Expand All @@ -139,11 +141,14 @@ test.serial('test sub', async (t) => {
},
([err, data]) => {
t.is(err, null);
t.true(Array.isArray(data));
t.is(data[0].text, 'test_sub');

t.is(data[0].increment, 15 + responseCount * 10);
responseCount++;
if (data && data.length > 0) {
t.true(Array.isArray(data));
t.is(data[0].text, 'test_sub');
t.is(data[0].increment, 15 + responseCount * 10);
responseCount++;
} else {
t.log(data);
}

if (responseCount >= 2) {
resolve();
Expand Down Expand Up @@ -233,3 +238,83 @@ test.serial('test events connect/disconnect without lazy connection', (t) => {
}, 100);
});
});

test.serial('subscribe to more returns unsub', (t) => {
t.timeout(1000);
const orm = t.context.orm;

return new Promise(async (resolve) => {
const unsub = await orm.subscribeToMore(
{
_om_test: {},
},
([err, data]) => {
t.is(err, null);
t.true(Array.isArray(data));

resolve();
},
);
t.true(typeof unsub === 'function');
});
});

test.serial('subscribe to more works', (t) => {
t.timeout(2000);
let events = 0;
const orm = t.context.orm;

return new Promise(async (resolve) => {
const unsub = await orm.subscribeToMore(
{
_om_test: {},
},
([err, data]) => {
events++;
t.is(err, null);
t.true(Array.isArray(data));

if (events === 2) resolve();
},
);
t.true(typeof unsub === 'function');

setTimeout(async () => {
await orm.mutate({
_om_test: {
insert: {
objects: {
text: 'test_sub',
increment: 15,
},
},
},
});
}, 1100);
});
});

test.serial('subscribe to more firstPass', (t) => {
t.timeout(2000);
let events = 0;
const orm = t.context.orm;

return new Promise(async (resolve) => {
const unsub = await orm.subscribeToMore(
{
_om_test: {},
},
([err, data]) => {
events++;
t.is(err, null);
t.true(Array.isArray(data));

if (events === 2) resolve();
},
{
passFirst: true,
},
);
t.true(typeof unsub === 'function');
});
});

0 comments on commit b53a79f

Please sign in to comment.