Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
perf(engage): refactor cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
batamar committed Apr 16, 2020
1 parent deefe38 commit 176e3ca
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 307 deletions.
44 changes: 1 addition & 43 deletions src/__tests__/engageMessageMutations.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as faker from 'faker';
import * as moment from 'moment';
import * as sinon from 'sinon';
import { MESSAGE_KINDS } from '../data/constants';
import * as engageUtils from '../data/resolvers/mutations/engageUtils';
Expand Down Expand Up @@ -30,7 +29,7 @@ import {
import * as messageBroker from '../messageBroker';

import { EngagesAPI } from '../data/dataSources';
import utils, { handleUnsubscription } from '../data/utils';
import { handleUnsubscription } from '../data/utils';
import { EMAIL_VALIDATION_STATUSES, KIND_CHOICES, STATUSES } from '../db/models/definitions/constants';
import './setup.ts';

Expand Down Expand Up @@ -130,7 +129,6 @@ describe('engage message mutation tests', () => {
type: 'year',
month: '2',
day: '14',
time: moment('2018-08-24T12:45:00'),
},
messenger: {
brandId: _brand._id,
Expand Down Expand Up @@ -408,7 +406,6 @@ describe('engage message mutation tests', () => {
type
day
month
time
}
segments {
_id
Expand Down Expand Up @@ -506,17 +503,6 @@ describe('engage message mutation tests', () => {
const editedUser = await userFactory();
const args = { ..._doc, _id: _message._id, fromUserId: editedUser._id };

try {
await graphqlRequest(mutation, 'engageMessageEdit', args);
} catch (e) {
expect(e[0].message).toBe(
'Error: Failed to connect crons api. Check CRONS_API_DOMAIN env or crons api is not running',
);
}

const fetchSpy = jest.spyOn(utils, 'fetchCronsApi');
fetchSpy.mockImplementation(() => Promise.resolve('ok'));

const engageMessage = await graphqlRequest(mutation, 'engageMessageEdit', args);

const tags = engageMessage.getTags.map(tag => tag._id);
Expand Down Expand Up @@ -544,8 +530,6 @@ describe('engage message mutation tests', () => {
expect(engageMessage.fromUser._id).toBe(args.fromUserId);

await graphqlRequest(mutation, 'engageMessageEdit', { ..._doc, _id: _message._id });

fetchSpy.mockRestore();
});

test('Remove engage message', async () => {
Expand All @@ -557,24 +541,11 @@ describe('engage message mutation tests', () => {
}
`;

try {
await graphqlRequest(mutation, 'engageMessageRemove', { _id: _message._id });
} catch (e) {
expect(e[0].message).toBe(
'Error: Failed to connect crons api. Check CRONS_API_DOMAIN env or crons api is not running',
);
}

const fetchSpy = jest.spyOn(utils, 'fetchCronsApi');
fetchSpy.mockImplementation(() => Promise.resolve('ok'));

_message = await engageMessageFactory({ kind: 'post' });

await graphqlRequest(mutation, 'engageMessageRemove', { _id: _message._id });

expect(await EngageMessages.findOne({ _id: _message._id })).toBe(null);

fetchSpy.mockRestore();
});

test('Set live engage message', async () => {
Expand All @@ -586,17 +557,6 @@ describe('engage message mutation tests', () => {
}
`;

try {
await graphqlRequest(mutation, 'engageMessageSetLive', { _id: _message._id });
} catch (e) {
expect(e[0].message).toBe(
'Error: Failed to connect crons api. Check CRONS_API_DOMAIN env or crons api is not running',
);
}

const fetchSpy = jest.spyOn(utils, 'fetchCronsApi');
fetchSpy.mockImplementation(() => Promise.resolve('ok'));

let response = await graphqlRequest(mutation, 'engageMessageSetLive', { _id: _message._id });

expect(response.isLive).toBe(true);
Expand All @@ -608,8 +568,6 @@ describe('engage message mutation tests', () => {
expect(response.isLive).toBe(true);

await graphqlRequest(mutation, 'engageMessageSetLive', { _id: _message._id });

fetchSpy.mockRestore();
});

test('Set pause engage message', async () => {
Expand Down
7 changes: 0 additions & 7 deletions src/__tests__/engageMessageQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { brandFactory, engageMessageFactory, segmentFactory, tagsFactory, userFa
import { Brands, EngageMessages, Segments, Tags, Users } from '../db/models';

import { EngagesAPI } from '../data/dataSources';
import utils from '../data/utils';
import './setup.ts';

describe('engageQueries', () => {
Expand Down Expand Up @@ -38,14 +37,10 @@ describe('engageQueries', () => {
}
`;

let fetchSpy;
let dataSources;

beforeEach(async () => {
dataSources = { EngagesAPI: new EngagesAPI() };

fetchSpy = jest.spyOn(utils, 'fetchCronsApi');
fetchSpy.mockImplementation(() => Promise.resolve('ok'));
});

afterEach(async () => {
Expand All @@ -55,8 +50,6 @@ describe('engageQueries', () => {
await Tags.deleteMany({});
await Brands.deleteMany({});
await Segments.deleteMany({});

fetchSpy.mockRestore();
});

test('Engage messages', async () => {
Expand Down
65 changes: 0 additions & 65 deletions src/__tests__/engageTracker.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/__tests__/integrationMutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { Brands, Customers, EmailDeliveries, Integrations, Users } from '../db/models';

import { IntegrationsAPI } from '../data/dataSources';
import utils from '../data/utils';
import { graphqlRequest } from '../db/connection';

describe('mutations', () => {
Expand Down Expand Up @@ -52,15 +51,11 @@ describe('mutations', () => {
};

let dataSources;
let fetchSpy;
let createAccountSpy;

beforeEach(async () => {
dataSources = { IntegrationsAPI: new IntegrationsAPI() };

fetchSpy = jest.spyOn(utils, 'fetchCronsApi');
fetchSpy.mockImplementation(() => Promise.resolve('ok'));

createAccountSpy = jest.spyOn(dataSources.IntegrationsAPI, 'createAccount');
createAccountSpy.mockImplementation(() => Promise.resolve());

Expand All @@ -79,7 +74,6 @@ describe('mutations', () => {
await EmailDeliveries.deleteMany({});
await Integrations.deleteMany({});

fetchSpy.mockRestore();
createAccountSpy.mockRestore();
});

Expand Down
Loading

0 comments on commit 176e3ca

Please sign in to comment.