Skip to content

Commit

Permalink
#1181 - introduced setup services to reuse across tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Dec 6, 2023
1 parent adcae13 commit 57470d8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
import BaseIntegrationTest from "./BaseIntegrationTest";
import {
AddressLevel,
ApprovalStatus,
Checklist,
ChecklistDetail,
ChecklistItem,
Concept,
ApprovalStatus, Concept,
CustomFilter,
Encounter,
EncounterType,
EntityApprovalStatus,
Form,
FormElement,
FormElementGroup,
FormMapping,
Gender,
Individual,
OrganisationConfig,
Program,
ProgramEncounter,
ProgramEnrolment,
ReportCard,
Settings,
StandardReportCardType,
SubjectType
} from "openchs-models";
import TestConceptFactory from "../test/model/TestConceptFactory";
import TestAddressLevelFactory from "../test/model/TestAddressLevelFactory";
import TestGenderFactory from "../test/model/TestGenderFactory";
import TestSettingsFactory from "../test/model/user/TestSettingsFactory";
import TestSubjectTypeFactory from "../test/model/TestSubjectTypeFactory";
import TestFormFactory from "../test/model/form/TestFormFactory";
import TestFormElementGroupFactory from "../test/model/form/TestFormElementGroupFactory";
import TestFormElementFactory from "../test/model/form/TestFormElementFactory";
import TestKeyValueFactory from "../test/model/TestKeyValueFactory";
import TestFormMappingFactory from "../test/model/form/TestFormMappingFactory";
import TestOrganisationConfigFactory from "../test/model/TestOrganisationConfigFactory";
import TestSubjectFactory from "../test/model/txn/TestSubjectFactory";
import TestObsFactory from "../test/model/TestObsFactory";
import ReportCardService from "../src/service/customDashboard/ReportCardService";
Expand All @@ -52,90 +30,30 @@ import moment from "moment";
import TestProgramFactory from '../test/model/TestProgramFactory';
import TestProgramEnrolmentFactory from '../test/model/txn/TestProgramEnrolmentFactory';
import TestProgramEncounterFactory from '../test/model/txn/TestProgramEncounterFactory';
import _ from 'lodash';
import TestChecklistService from "./service/TestChecklistService";
import TestOrganisationService from "./service/TestOrganisationService";
import TestConceptFactory from "../test/model/TestConceptFactory";

function getCount(test, card, reportFilters) {
return test.reportCardService.getReportCardCount(card, reportFilters).primaryValue
}

function createChecklist(programEnrolment, db, withDue = true) {
const checklistConcept = db.create(Concept, TestConceptFactory.createWithDefaults({dataType: Concept.dataType.Text}));
const checklistDetail = db.create(ChecklistDetail, {
uuid: General.randomUUID(),
name: 'ck-detail',
items: [],
voided: false})
const checklist = {
uuid: General.randomUUID(),
items: [
{
uuid: General.randomUUID(),
detail: {
uuid: General.randomUUID(),
concept: checklistConcept,
stateConfig: [{
state: "Due",
from: {key: "key1", value: 1},
to: {key: "key2", value: 2},
color: "red",
displayOrder: 3,
start: -1,
end: +1
}],
checklistDetail: checklistDetail,
}
}]
}
let checklistToBeCreated = Checklist.create();
checklistToBeCreated.uuid = _.isNil(checklist.uuid) ? checklistToBeCreated.uuid : checklist.uuid;
checklistToBeCreated.baseDate = withDue ? moment().toDate() : moment().add(-2, "day").toDate();
checklistToBeCreated.detail = checklistDetail;
const savedChecklist = db.create(Checklist, checklistToBeCreated, true);
const checklistItems = checklist.items.map((item) => {
const checklistItem = ChecklistItem.create({
uuid: item.uuid,
checklist: savedChecklist,
detail: item.detail
});
const savedChecklistItem = db.create(ChecklistItem, checklistItem, true);
return savedChecklistItem;
});
checklistItems.forEach(ci => savedChecklist.items.push(ci));
programEnrolment.addChecklist(savedChecklist);
savedChecklist.programEnrolment = programEnrolment;
}

class ReportCardServiceIntegrationTest extends BaseIntegrationTest {

setup() {
super.setup();
this.executeInWrite((db) => {
this.organisationData = TestOrganisationService.setupOrganisation(db);
this.concept = db.create(Concept, TestConceptFactory.createWithDefaults({dataType: Concept.dataType.Text}));
this.addressLevel = db.create(AddressLevel, TestAddressLevelFactory.createWithDefaults({level: 1}));
this.addressLevel2 = db.create(AddressLevel, TestAddressLevelFactory.createWithDefaults({level: 1}));
this.gender = db.create(Gender, TestGenderFactory.createWithDefaults({name: "Male"}));
db.create(Settings, TestSettingsFactory.createWithDefaults({}));

this.subjectType = db.create(SubjectType, TestSubjectTypeFactory.createWithDefaults({type: SubjectType.types.Person, name: 'Beneficiary'}));
const program = db.create(Program, TestProgramFactory.create({name: 'Child'}));
const programEncounterType = db.create(EncounterType, TestEncounterTypeFactory.create({name: "Birth form"}));
const encounterType = db.create(EncounterType, TestEncounterTypeFactory.create({name: "Bar"}));
const form = db.create(Form, TestFormFactory.createWithDefaults({formType: Form.formTypes.IndividualProfile}));
const formElementGroup = db.create(FormElementGroup, TestFormElementGroupFactory.create({form: form}));
db.create(FormElement, TestFormElementFactory.create({
uuid: "FOO",
concept: this.concept,
displayOrder: 1,
formElementGroup: formElementGroup,
mandatory: true,
keyValues: [TestKeyValueFactory.create({key: "unique", value: "true"})]
}));
db.create(FormMapping, TestFormMappingFactory.createWithDefaults({subjectType: this.subjectType, form: form}));
db.create(OrganisationConfig, TestOrganisationConfigFactory.createWithDefaults({}));
const approvalStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({}));
const enrolmentApprovalStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({}));
const pendingStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({status: ApprovalStatus.statuses.Pending}));
const programEncRejectedStatus = db.create(ApprovalStatus, TestApprovalStatusFactory.create({status: ApprovalStatus.statuses.Rejected}));

const subject1Id = General.randomUUID();
const subject2Id = General.randomUUID();
const encounterId1 = General.randomUUID();
Expand All @@ -160,7 +78,7 @@ class ReportCardServiceIntegrationTest extends BaseIntegrationTest {
const subject1 = db.create(Individual, TestSubjectFactory.createWithDefaults({
uuid: subject1Id,
subjectType: this.subjectType,
address: this.addressLevel,
address: this.organisationData.addressLevel,
firstName: "foo",
lastName: "bar",
observations: [TestObsFactory.create({concept: this.concept, valueJSON: JSON.stringify(this.concept.getValueWrapperFor("ABC"))})],
Expand Down Expand Up @@ -197,7 +115,7 @@ class ReportCardServiceIntegrationTest extends BaseIntegrationTest {
approvalStatuses: []
}));

createChecklist( programEnrolment1, db);
TestChecklistService.createChecklist( programEnrolment1, db);

const subject2EAS = db.create(EntityApprovalStatus, TestEntityApprovalStatusFactory.create({
entityType: EntityApprovalStatus.entityType.Subject,
Expand All @@ -208,7 +126,7 @@ class ReportCardServiceIntegrationTest extends BaseIntegrationTest {
const subject2 = db.create(Individual, TestSubjectFactory.createWithDefaults({
uuid: subject2Id,
subjectType: this.subjectType,
address: this.addressLevel2,
address: this.organisationData.addressLevel2,
firstName: "foo2",
lastName: "bar2",
observations: [TestObsFactory.create({concept: this.concept, valueJSON: JSON.stringify(this.concept.getValueWrapperFor("DEF"))})],
Expand Down Expand Up @@ -238,7 +156,7 @@ class ReportCardServiceIntegrationTest extends BaseIntegrationTest {
approvalStatuses: [enrolmentEAS]
}));

createChecklist(programEnrolment2, db, false);
TestChecklistService.createChecklist(programEnrolment2, db, false);

programEnrolment2.addEncounter(db.create(ProgramEncounter, TestProgramEncounterFactory.create({
uuid: programEncounterId1,
Expand Down Expand Up @@ -283,13 +201,11 @@ class ReportCardServiceIntegrationTest extends BaseIntegrationTest {
});

this.reportCardService = this.getService(ReportCardService);
this.addressSelected = TestDashboardReportFilterFactory.create({type: CustomFilter.type.Address, filterValue: [this.addressLevel]});
this.address2Selected = TestDashboardReportFilterFactory.create({type: CustomFilter.type.Address, filterValue: [this.addressLevel2]});
this.twoAddressSelected = TestDashboardReportFilterFactory.create({type: CustomFilter.type.Address, filterValue: [this.addressLevel, this.addressLevel2]});
this.addressSelected = TestDashboardReportFilterFactory.create({type: CustomFilter.type.Address, filterValue: [this.organisationData.addressLevel]});
this.address2Selected = TestDashboardReportFilterFactory.create({type: CustomFilter.type.Address, filterValue: [this.organisationData.addressLevel2]});
this.twoAddressSelected = TestDashboardReportFilterFactory.create({type: CustomFilter.type.Address, filterValue: [this.organisationData.addressLevel, this.organisationData.addressLevel2]});
}



getResultForApprovalCardsType() {
assert.equal(1, getCount(this, this.approvedCard, []));
assert.equal(1, getCount(this, this.approvedCard, [this.addressSelected]));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import _ from "lodash";
import {Checklist, ChecklistDetail, ChecklistItem, Concept} from "openchs-models";
import TestConceptFactory from "../../test/model/TestConceptFactory";
import General from "../../src/utility/General";
import moment from "moment/moment";

class TestChecklistService {
static createChecklist(programEnrolment, db, withDue = true) {
const checklistConcept = db.create(Concept, TestConceptFactory.createWithDefaults({dataType: Concept.dataType.Text}));
const checklistDetail = db.create(ChecklistDetail, {
uuid: General.randomUUID(),
name: 'ck-detail',
items: [],
voided: false})
const checklist = {
uuid: General.randomUUID(),
items: [
{
uuid: General.randomUUID(),
detail: {
uuid: General.randomUUID(),
concept: checklistConcept,
stateConfig: [{
state: "Due",
from: {key: "key1", value: 1},
to: {key: "key2", value: 2},
color: "red",
displayOrder: 3,
start: -1,
end: +1
}],
checklistDetail: checklistDetail,
}
}]
}
let checklistToBeCreated = Checklist.create();
checklistToBeCreated.uuid = _.isNil(checklist.uuid) ? checklistToBeCreated.uuid : checklist.uuid;
checklistToBeCreated.baseDate = withDue ? moment().toDate() : moment().add(-2, "day").toDate();
checklistToBeCreated.detail = checklistDetail;
const savedChecklist = db.create(Checklist, checklistToBeCreated, true);
const checklistItems = checklist.items.map((item) => {
const checklistItem = ChecklistItem.create({
uuid: item.uuid,
checklist: savedChecklist,
detail: item.detail
});
return db.create(ChecklistItem, checklistItem, true);
});
checklistItems.forEach(ci => savedChecklist.items.push(ci));
programEnrolment.addChecklist(savedChecklist);
savedChecklist.programEnrolment = programEnrolment;
}
}

export default TestChecklistService;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {AddressLevel, Gender, OrganisationConfig, Settings} from "openchs-models";
import TestAddressLevelFactory from "../../test/model/TestAddressLevelFactory";
import TestGenderFactory from "../../test/model/TestGenderFactory";
import TestSettingsFactory from "../../test/model/user/TestSettingsFactory";
import TestOrganisationConfigFactory from "../../test/model/TestOrganisationConfigFactory";

class TestOrganisationService {
static setupOrganisation(db) {
const returnData = {};
returnData.addressLevel = db.create(AddressLevel, TestAddressLevelFactory.createWithDefaults({level: 1}));
returnData.addressLevel2 = db.create(AddressLevel, TestAddressLevelFactory.createWithDefaults({level: 1}));
returnData.gender = db.create(Gender, TestGenderFactory.createWithDefaults({name: "Male"}));
db.create(Settings, TestSettingsFactory.createWithDefaults({}));
db.create(OrganisationConfig, TestOrganisationConfigFactory.createWithDefaults({}));
return returnData;
}
}

export default TestOrganisationService;
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CustomDashboardActions {
const standardReportCardType = reportCard.standardReportCardType;
const viewName = CustomDashboardActions._getViewName(standardReportCardType);
if (!_.isNil(result)) {
setTimeout(() => action.cb(result, result.length, status, viewName), 0);
setTimeout(() => action.onApprovalItemsResults(result, result.length, status, viewName), 0);
}
}
return newState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class CustomDashboardView extends AbstractComponent {
indicatorActionName: Actions.LOAD_INDICATOR
}).to(TaskListView);
},
cb: (results, count, status, viewName) => TypedTransition.from(this).with({
onApprovalItemsResults: (results, count, status, viewName) => TypedTransition.from(this).with({
indicatorActionName: Actions.LOAD_INDICATOR,
headerTitle: status || 'subjectsList',
results: results,
Expand Down

0 comments on commit 57470d8

Please sign in to comment.