import { expect } from "chai"; import * as e2e from '../e2e_helper' import { TestApolloClient } from '../e2e_helper'; import { gql } from "apollo-server-core" import { runGQL } from "../e2e_helper"; describe("Test subscritions", function () { this.timeout(50000) let gqlClient : e2e.TestApolloClient; let headers = { 'x-device-id': '123testDeviceId', } beforeEach(async () => { gqlClient = new TestApolloClient({ host: "127.0.0.1", port: "42772" }); }); // afterEach(() => gqlClient.stop()) it('maintenance', async () => { const sub = e2e.createSubscription({ client: gqlClient, query: gql` subscription { maintenance { description } } ` }); await Maintenance.create({description: "test", enable: true}).fetch() let len = await Maintenance.count() await sub.waitForResults(); expect(sub.results).to.be.of.length(1); await Maintenance.destroy({}).fetch() await sub.waitForResults({len:len}); expect(sub.results).to.be.of.length(len+1); expect(sub.results[1].data.maintenance).to.equal(null); sub.disconnect(); // unsubscribe }) it('order', async () => { const query = `{order(orderId:"test-subscription-cart"){ id total }}`; await runGQL(query, headers); const sub = e2e.createSubscription({ client: gqlClient, query: gql` subscription { order(deviceId:"${headers["x-device-id"]}") { dishesCount } } ` }); const query_orderAddDish = `#graphql mutation { orderAddDish(dishId: "df19cbbc-c026-595e-9296-1c8a378da6b3", orderId:"test-subscription-cart", amount: 7){ dishes { dish { id name price } itemTotal } total } }`; await runGQL(query_orderAddDish, headers); await sub.waitForResults(); expect(sub.results[0].data.order.dishesCount).to.equal(7) sub.disconnect(); // unsubscribe }) it('action', async () => { const eventHelper: typeof import("../../lib/eventHelper") = require("../../lib/eventHelper"); const sub = e2e.createSubscription({ client: gqlClient, query: gql` subscription { action(deviceId:"${headers["x-device-id"]}") { id type data } } ` }); await sub.wait(700); const action = { deviceId: headers["x-device-id"], type: "test", data: { test: true, }, }; let event = eventHelper.sendAction(action); await sub.waitForResults(); expect(sub.results).to.be.of.length(1); expect(sub.results[0].data.action.id).be.not.null; expect(sub.results[0].data.action.data.test).to.equal(true); sub.disconnect(); // unsubscribe }) it('message', async () => { const eventHelper: typeof import("../../lib/eventHelper") = require("../../lib/eventHelper"); const sub = e2e.createSubscription({ client: gqlClient, query: gql` subscription { message(deviceId:"${headers["x-device-id"]}") { id title type message } } ` }); await sub.wait(700); let message = { deviceId: headers["x-device-id"], type: "info" as "info", title: "TEST", message: "TEST", }; eventHelper.sendMessage(message); await sub.waitForResults(); expect(sub.results).to.be.of.length(1); expect(sub.results[0].data.message.id).be.not.null; expect(sub.results[0].data.message.title).to.equal("TEST"); expect(sub.results[0].data.message.message).to.equal("TEST"); sub.disconnect(); // unsubscribe }) })