Skip to content

Commit

Permalink
chore(#8700): clean up and organize e2e tests folders /service-worker…
Browse files Browse the repository at this point in the history
…, /sms, /targets, /tasks (#9450)
  • Loading branch information
tatilepizs authored Oct 14, 2024
1 parent 07314e4 commit 61c9dc2
Show file tree
Hide file tree
Showing 17 changed files with 375 additions and 517 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Old Navigation', () => {
it('should navigate to the Messages section and open a sent message', async () => {
const message = 'Navigations test';
await oldNavigationPage.goToMessages();
await messagesPage.sendMessage(message, person.name, person.phone );
await messagesPage.sendMessageDesktop(message, person.name, person.phone );
await messagesPage.openMessage(person._id);

const { name, phone } = await messagesPage.getMessageHeader();
Expand Down
149 changes: 69 additions & 80 deletions tests/e2e/default/service-worker/service-worker.wdio-spec.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,86 @@
const { expect } = require('chai');
const URL = require('url');
const utils = require('@utils');
const loginPage = require('@page-objects/default/login/login.wdio.page');
const commonPage = require('@page-objects/default/common/common.wdio.page');
const placeFactory = require('@factories/cht/contacts/place');
const userFactory = require('@factories/cht/users/users');

// global caches fetch Response navigator
const getCachedRequests = async (raw) => {
const cacheDetails = await browser.executeAsync(async (callback) => {
describe('Service worker cache', () => {
const DEFAULT_TRANSLATIONS = {
'sync.now': 'Sync now',
'sidebar_menu.title': 'Menu',
'sync.status.not_required': 'All reports synced',
};

// global caches fetch Response navigator
const getCachedRequests = async (raw) => {
const cacheDetails = await browser.executeAsync(async (callback) => {
const cacheNames = await caches.keys();
const cache = await caches.open(cacheNames[0]);
const cachedRequests = await cache.keys();
const cachedRequestSummary = cachedRequests.map(req => ({ url: req.url }));
callback({
name: cacheNames[0],
requests: cachedRequestSummary,
});
});

if (raw) {
return cacheDetails;
}

const urls = cacheDetails.requests.map(request => URL.parse(request.url).pathname);
urls.sort();
return { name: cacheDetails.name, urls };
};

const stubAllCachedRequests = () => browser.executeAsync(async (callback) => {
const cacheNames = await caches.keys();
const cache = await caches.open(cacheNames[0]);
const cachedRequests = await cache.keys();
const cachedRequestSummary = cachedRequests.map(req => ({ url: req.url }));
await Promise.all(cachedRequests.map(request => cache.put(request, new Response('cache'))));
callback();
});

const doFetch = (path, headers) => browser.executeAsync(async (innerPath, innerHeaders, callback) => {
const result = await fetch(innerPath, { headers: innerHeaders });
callback({
name: cacheNames[0],
requests: cachedRequestSummary,
body: await result.text(),
ok: result.ok,
status: result.status,
});
});
}, path, headers);

if (raw) {
return cacheDetails;
}

const urls = cacheDetails.requests.map(request => URL.parse(request.url).pathname);
urls.sort();
return { name: cacheDetails.name, urls };
};

const stubAllCachedRequests = () => browser.executeAsync(async (callback) => {
const cacheNames = await caches.keys();
const cache = await caches.open(cacheNames[0]);
const cachedRequests = await cache.keys();
await Promise.all(cachedRequests.map(request => cache.put(request, new Response('cache'))));
callback();
});
const unregisterServiceWorkerAndWipeAllCaches = () => browser.executeAsync(async (callback) => {
const registrations = await navigator.serviceWorker.getRegistrations();
registrations.forEach(registration => registration.unregister());

const doFetch = (path, headers) => browser.executeAsync(async (innerPath, innerHeaders, callback) => {
const result = await fetch(innerPath, { headers: innerHeaders });
callback({
body: await result.text(),
ok: result.ok,
status: result.status,
});
}, path, headers);
const cacheNames = await caches.keys();
for (const name of cacheNames) {
await caches.delete(name);
}

const unregisterServiceWorkerAndWipeAllCaches = () => browser.executeAsync(async (callback) => {
const registrations = await navigator.serviceWorker.getRegistrations();
registrations.forEach(registration => registration.unregister());
callback();
});

const cacheNames = await caches.keys();
for (const name of cacheNames) {
await caches.delete(name);
}
const district = placeFactory.generateHierarchy(['district_hospital']).get('district_hospital');
const chw = userFactory.build({ place: district._id });

callback();
});
const login = async () => {
await browser.throttle('online');
await loginPage.login(chw);
await commonPage.waitForPageLoaded();
};

const district = {
_id: 'fixture:district',
type: 'district_hospital',
name: 'District',
place_id: 'district',
reported_date: new Date().getTime(),
};

const chw = {
username: 'bob',
password: 'medic.123',
place: 'fixture:district',
contact: { _id: 'fixture:user:bob', name: 'Bob' },
roles: ['chw'],
};

const login = async () => {
await browser.throttle('online');
await loginPage.login(chw);
await commonPage.waitForPageLoaded();
};

const isLoggedIn = async () => {
const tab = await commonPage.messagesTab();
return await tab.isExisting();
};

const loginIfNeeded = async () => {
if (!await isLoggedIn()) {
await login();
}
};
const isLoggedIn = async () => {
const tab = await commonPage.messagesTab();
return await tab.isExisting();
};

describe('Service worker cache', () => {
const DEFAULT_TRANSLATIONS = {
'sync.now': 'Sync now',
'sidebar_menu.title': 'Menu',
'sync.status.not_required': 'All reports synced',
const loginIfNeeded = async () => {
if (!await isLoggedIn()) {
await login();
}
};

before(async () => {
Expand All @@ -105,8 +93,9 @@ describe('Service worker cache', () => {
await loginIfNeeded();
});

afterEach(async () => {
await utils.revertSettings(true);
after(async () => {
await utils.deleteUsers([chw]);
await utils.revertDb([/^form:/], true);
});

it('confirm initial list of cached resources', async () => {
Expand Down
14 changes: 7 additions & 7 deletions tests/e2e/default/sms/africas-talking.wdio-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const loginPage = require('@page-objects//default/login/login.wdio.page');
const reportsPage = require('@page-objects/default/reports/reports.wdio.page');
const smsPregnancy = require('@factories/cht/reports/sms-pregnancy');

const CREDENTIAL_PASS = 'yabbadabbadoo';
const CREDENTIAL_KEY = 'africastalking.com:incoming';
describe('Africas Talking api', () => {
const CREDENTIAL_PASS = 'yabbadabbadoo';
const CREDENTIAL_KEY = 'africastalking.com:incoming';

describe('africas talking api', () => {
before( () => utils.saveCredentials(CREDENTIAL_KEY, CREDENTIAL_PASS) );

describe('- gateway submits new WT sms messages', () => {
describe('Gateway submits new WT sms messages', () => {
const submitSms = body => {
const content = querystring.stringify(body);
return utils.request({
Expand All @@ -32,7 +32,7 @@ describe('africas talking api', () => {
await loginPage.cookieLogin();
});

it('- shows content', async () => {
it('should shows content', async () => {
const rawNumber = '+64271234567';
const message = 'Hello';

Expand All @@ -55,7 +55,7 @@ describe('africas talking api', () => {

});

describe('- gateway submits WT sms status updates', () => {
describe('Gateway submits WT sms status updates', () => {
let savedDoc;
let testReport;

Expand Down Expand Up @@ -92,7 +92,7 @@ describe('africas talking api', () => {

afterEach(() => utils.deleteDoc(savedDoc));

it('- shows content', async () => {
it('should shows content', async () => {
await commonPage.goToReports();
const firstReport = await reportsPage.leftPanelSelectors.firstReport();
const firstReportInfo = await reportsPage.getListReportInfo(firstReport);
Expand Down
12 changes: 8 additions & 4 deletions tests/e2e/default/sms/export.wdio-spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const moment = require('moment');

const utils = require('@utils');
const messagesPage = require('@page-objects/default/sms/messages.wdio.page');
const userFactory = require('@factories/cht/users/users');
const placeFactory = require('@factories/cht/contacts/place');
const personFactory = require('@factories/cht/contacts/person');
const commonElements = require('@page-objects/default/common/common.wdio.page');
const loginPage = require('@page-objects/default/login/login.wdio.page');
const fileDownloadUtils = require('@utils/file-download');
const utils = require('@utils');

describe('Export Messages', () => {
const places = placeFactory.generateHierarchy();
Expand All @@ -28,8 +27,13 @@ describe('Export Messages', () => {
await commonElements.goToMessages();
});

it('Should download export file', async () => {
await messagesPage.sendMessage('It is working!', patient.phone, patient.name);
after(async () => {
await utils.deleteUsers([onlineUser]);
await utils.revertDb([/^form:/], true);
});

it('should download export file', async () => {
await messagesPage.sendMessageDesktop('It is working!', patient.phone, patient.name);
await messagesPage.exportMessages();

const files = await fileDownloadUtils.waitForDownload(`messages-${today.format('YYYYMMDD')}`);
Expand Down
28 changes: 14 additions & 14 deletions tests/e2e/default/sms/gateway.wdio-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ const commonElements = require('@page-objects/default/common/common.wdio.page');
const loginPage = require('@page-objects/default/login/login.wdio.page');
const smsPregnancy = require('@factories/cht/reports/sms-pregnancy');

const pollSmsApi = body => {
return utils.request({
method: 'POST',
path: '/api/sms',
body: body
});
};

describe('sms-gateway api', () => {
const pollSmsApi = body => {
return utils.request({
method: 'POST',
path: '/api/sms',
body: body
});
};

before(async () => {
await loginPage.cookieLogin();
await commonElements.waitForPageLoaded();
});

describe('- gateway submits new WT sms messages', () => {
describe('Gateway submits new WT sms messages', () => {
beforeEach(async () => {
const body = {
messages: [
Expand All @@ -33,7 +33,7 @@ describe('sms-gateway api', () => {
await pollSmsApi(body);
});

it('shows content', async () => {
it('should shows content', async () => {
//LHS
const phone = '+64271234567';
const msg = 'hello';
Expand All @@ -53,7 +53,7 @@ describe('sms-gateway api', () => {
});
});

describe('- gateway submits WT sms status updates', () => {
describe('Gateway submits WT sms status updates', () => {
let savedDoc;

beforeEach(async () => {
Expand All @@ -74,7 +74,7 @@ describe('sms-gateway api', () => {
await pollSmsApi(body);
});

it('- shows content', async () => {
it('should shows content', async () => {

await reportsPage.goToReportById(savedDoc);

Expand All @@ -90,7 +90,7 @@ describe('sms-gateway api', () => {
});
});

describe('- api returns list of pending WO messages', () => {
describe('api returns list of pending WO messages', () => {
let savedDoc;
let response;
let reportWithTwoMessagesToSend;
Expand All @@ -109,7 +109,7 @@ describe('sms-gateway api', () => {
response = await pollSmsApi({});
});

it('- returns list and updates state', async () => {
it('should returns list and updates state', async () => {
const scheduledTaskMessage = reportWithTwoMessagesToSend.scheduled_tasks[0].messages[0];
const taskMessage = reportWithTwoMessagesToSend.tasks[0].messages[0];
expect(response.messages.length).to.equal(2);
Expand Down
41 changes: 16 additions & 25 deletions tests/e2e/default/sms/link-to-patient.wdio-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,31 @@ const commonPage = require('@page-objects/default/common/common.wdio.page');
const contactPage = require('@page-objects/default/contacts/contacts.wdio.page');
const sentinelUtils = require('@utils/sentinel');

const formId = 'CASEID';
const formTitle = 'Case Id Form';

const places = placeFactory.generateHierarchy();
const hcId = places.get('health_center')._id;

const user = userFactory.build({ place: hcId });

const forms = {
CASEID: {
meta: { code: formId, icon: 'icon-healthcare', translation_key: formTitle },
fields: {}
}
};

const registrations = [{
form: formId, events: [{ name: 'on_create', trigger: 'add_case' }]
}];

const transitions = {
self_report: true
};
describe('Link SMS to patient without passing id', () => {
const formId = 'CASEID';
const formTitle = 'Case Id Form';

const self_report = [{ form: formId }];
const places = placeFactory.generateHierarchy();
const hcId = places.get('health_center')._id;
const user = userFactory.build({ place: hcId });

const docs = [...places.values(), user];
const forms = { CASEID: { meta: { code: formId, icon: 'icon-healthcare', translation_key: formTitle }, fields: {} } };
const registrations = [{ form: formId, events: [{ name: 'on_create', trigger: 'add_case' }] }];
const transitions = { self_report: true };
const self_report = [{ form: formId }];
const docs = [...places.values(), user];

describe('Link SMS to patient without passing id', () => {
before(async () => {
await utils.saveDocs(docs);
await utils.updateSettings({ forms, registrations, transitions, self_report }, { ignoreReload: true });
await loginPage.cookieLogin();
});

after(async () => {
await utils.revertSettings(true);
await utils.revertDb([/^form:/], true);
});

it('Send SMS without patient_id and report created under person', async () => {
await utils.createUsers([user]);
await sentinelUtils.waitForSentinel();
Expand Down
Loading

0 comments on commit 61c9dc2

Please sign in to comment.