Skip to content

Commit

Permalink
Add tests for which techniques display
Browse files Browse the repository at this point in the history
  • Loading branch information
simonfernandes authored and deepaksftc committed Aug 12, 2024
1 parent 0357868 commit 498baff
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 12 deletions.
3 changes: 2 additions & 1 deletion apps/backend/src/datasources/TechniqueDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export interface TechniqueDataSource {
first?: number,
offset?: number
): Promise<{ totalCount: number; techniques: Technique[] }>;
getTechniquesByIds(techniqueIds: number[]): Promise<Technique[]>;
getInstrumentsByTechniqueId(techniqueId: number): Promise<Instrument[]>;
getTechniquesByInstrumentIds(instrumentIds: number[]): Promise<Technique[]>;
update(technique: Technique): Promise<Technique>;
delete(techniqueId: number): Promise<Technique>;
assignInstrumentsToTechnique(
Expand All @@ -24,5 +26,4 @@ export interface TechniqueDataSource {
proposalPk: number,
techniqueIds: number[]
): Promise<boolean>;
getTechniquesByIds(techniqueIds: number[]): Promise<Technique[]>;
}
10 changes: 10 additions & 0 deletions apps/backend/src/datasources/mockups/TechniqueDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ export class TechniqueDataSourceMock implements TechniqueDataSource {

return [];
}

async getTechniquesByInstrumentIds(
instrumentIds: number[]
): Promise<Technique[]> {
if (instrumentIds) {
return dummyTechniques;
}

return [];
}
}
15 changes: 15 additions & 0 deletions apps/backend/src/datasources/postgres/TechniqueDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,19 @@ export default class PostgresTechniqueDataSource
throw new Error(`Error getting techniques: ${error}`);
});
}

async getTechniquesByInstrumentIds(
instrumentIds: number[]
): Promise<Technique[]> {
try {
const uniqueTechniqueIds = await database('technique_has_instruments')
.whereIn('instrument_id', instrumentIds)
.distinct()
.pluck('technique_id');

return this.getTechniquesByIds(uniqueTechniqueIds);
} catch (error) {
throw new Error(`Error getting techniques by instrument IDs: ${error}`);
}
}
}
15 changes: 13 additions & 2 deletions apps/backend/src/models/questionTypes/TechniquePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,26 @@ export const techniquePickerDefinition: Question<DataType.TECHNIQUE_PICKER> = {
transformConfig: async (config, callId) => {
const fallBackConfig = { ...config, techniques: [] };
try {
if (!callId) return fallBackConfig;

const techniqueDataSource = container.resolve<TechniqueDataSource>(
Tokens.TechniqueDataSource
);
const instrumentDataSource = container.resolve<InstrumentDataSource>(
Tokens.InstrumentDataSource
);

const techniques = await techniqueDataSource.getTechniques();
const instrumentsOnCall =
await instrumentDataSource.getInstrumentsByCallId([callId]);

const uniqueTechniques =
await techniqueDataSource.getTechniquesByInstrumentIds(
instrumentsOnCall.map((inst) => inst.id)
);

return {
...config,
techniques: techniques.techniques.map(
techniques: uniqueTechniques.map(
(technique) => new TechniqueOptionClass(technique.id, technique.name)
),
};
Expand Down
138 changes: 130 additions & 8 deletions apps/e2e/cypress/e2e/techniques.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ context('Technique tests', () => {
description: faker.word.words(5),
};

const technique3 = {
name: faker.word.words(1),
shortCode: faker.string.alphanumeric(15),
description: faker.word.words(5),
};

const scientist1 = initialDBData.users.user1;
const scientist2 = initialDBData.users.user2;
const scientist3 = initialDBData.users.user3;
const scientist4 = initialDBData.users.placeholderUser;
const scientist5 = initialDBData.users.reviewer;

const instrument1 = {
name: faker.word.words(1),
Expand All @@ -48,6 +56,20 @@ context('Technique tests', () => {
managerUserId: scientist3.id,
};

const instrument4 = {
name: faker.word.words(1),
shortCode: faker.string.alphanumeric(15),
description: faker.word.words(5),
managerUserId: scientist4.id,
};

const instrument5 = {
name: faker.word.words(1),
shortCode: faker.string.alphanumeric(15),
description: faker.word.words(5),
managerUserId: scientist5.id,
};

const proposalWorkflow = {
name: faker.word.words(1),
description: faker.word.words(5),
Expand Down Expand Up @@ -145,13 +167,6 @@ context('Technique tests', () => {

cy.contains(technique1.name).should('not.exist');
});
});

describe('Advanced techniques tests as user officer role', () => {
beforeEach(() => {
cy.login('officer');
cy.visit('/');
});

it('User officer should be able to assign and unassign instruments to technique without page refresh', function () {
cy.createTechnique(technique1);
Expand Down Expand Up @@ -232,16 +247,20 @@ context('Technique tests', () => {
});
});

describe('Technique picker automatic instrument assignment', () => {
describe('Advanced techniques tests as user officer role', () => {
let techniqueId1: number;
let techniqueId2: number;
let techniqueId3: number;

let techniqueName1: string;
let techniqueName2: string;
let techniqueName3: string;

let instrumentId1: number;
let instrumentId2: number;
let instrumentId3: number;
let instrumentId4: number;
let instrumentId5: number;

let topicId: number;

Expand All @@ -254,10 +273,12 @@ context('Technique tests', () => {
});
cy.resetDB();
cy.getAndStoreFeaturesEnabled();

cy.createTemplate({
name: 'Proposal Template with Technique Picker',
groupId: TemplateGroupId.PROPOSAL,
});

cy.createProposalWorkflow({
name: proposalWorkflow.name,
description: proposalWorkflow.description,
Expand All @@ -280,13 +301,15 @@ context('Technique tests', () => {
instrumentFapIds: [{ instrumentId: instrumentId1 }],
});
});

cy.createInstrument(instrument2).then((result) => {
instrumentId2 = result.createInstrument.id;
cy.assignInstrumentToCall({
callId: initialDBData.call.id,
instrumentFapIds: [{ instrumentId: instrumentId2 }],
});
});

cy.createInstrument(instrument3).then((result) => {
instrumentId3 = result.createInstrument.id;
cy.assignInstrumentToCall({
Expand Down Expand Up @@ -377,7 +400,9 @@ context('Technique tests', () => {

cy.contains(title).parent().contains(instrument1.name);
cy.contains(title).parent().contains(instrument2.name);

cy.contains(title).parent().find('[aria-label="View proposal"]').click();

cy.contains('td', instrument1.name).should('exist');
cy.contains('td', instrument2.name).should('exist');
});
Expand Down Expand Up @@ -433,10 +458,107 @@ context('Technique tests', () => {
cy.contains(title).parent().contains(instrument1.name);
cy.contains(title).parent().contains(instrument2.name);
cy.contains(title).parent().contains(instrument3.name);

cy.contains(title).parent().find('[aria-label="View proposal"]').click();

cy.contains('td', instrument1.name).should('exist');
cy.contains('td', instrument2.name).should('exist');
cy.contains('td', instrument3.name).should('exist');
});

it('When instruments are assigned to multiple techniques, only unique techniques are shown in the questionnaire', function () {
// Instruments 1 and 2 are now assigned to both techniques 1 and 2
cy.assignInstrumentsToTechnique({
instrumentIds: [instrumentId1, instrumentId2],
techniqueId: techniqueId2,
});

cy.login('user1', initialDBData.roles.user);
cy.visit('/');
cy.finishedLoading();

cy.contains('New Proposal').click();
cy.get('[data-cy=call-list]').find('li:first-child').click();
cy.get('[data-cy=title] input').type(title).should('have.value', title);
cy.get('[data-cy=abstract] textarea')
.first()
.type(abstract)
.should('have.value', abstract);
cy.contains('Save and continue').click();
cy.finishedLoading();

cy.get('[data-natural-key^="technique_picker"]').click();

cy.get('[role="option"]')
.should('have.length', 2)
.then((options) => {
const techniques = [...options].map((option) => option.textContent);
expect(techniques).to.include.members([
techniqueName1,
techniqueName2,
]);
});
});

it('Techniques options display in the questionnaire based on the instruments attached to the call', function () {
cy.createTechnique(technique3).then((result) => {
techniqueName3 = result.createTechnique.name;
techniqueId3 = result.createTechnique.id;
});

cy.createInstrument(instrument4).then((result) => {
instrumentId4 = result.createInstrument.id;

cy.assignInstrumentsToTechnique({
instrumentIds: [instrumentId4],
techniqueId: techniqueId3,
});
});

cy.createInstrument(instrument5).then((result) => {
instrumentId5 = result.createInstrument.id;

cy.assignInstrumentsToTechnique({
instrumentIds: [instrumentId5],
techniqueId: techniqueId1,
});
});

cy.login('user1', initialDBData.roles.user);
cy.visit('/');
cy.finishedLoading();

cy.contains('New Proposal').click();
cy.get('[data-cy=call-list]').find('li:first-child').click();
cy.get('[data-cy=title] input').type(title).should('have.value', title);
cy.get('[data-cy=abstract] textarea')
.first()
.type(abstract)
.should('have.value', abstract);
cy.contains('Save and continue').click();
cy.finishedLoading();

cy.get('[data-natural-key^="technique_picker"]').click();

cy.get('[role="option"]')
.should('have.length', 2)
.then((options) => {
const techniques = [...options].map((option) => option.textContent);
/*
Technique 1 has an instrument 5 not attached to the call, but should
still show as there are other instruments attached. Technique 2
has all attached and should show.
*/
expect(techniques).to.include.members([
techniqueName1,
techniqueName2,
]);
/*
Technique 3 contains a single instrument 4 that is not attached
to the call, so it should therefore not show as an option.
*/
expect(techniques).to.not.include.members([techniqueName3]);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export function QuestionaryComponentTechniquePicker(
data-natural-key={naturalKey}
data-cy="dropdown-ul"
>
{!config.isMultipleSelect && <MenuItem value={0}>None</MenuItem>}
{config.techniques.map((technique) => {
return (
<SelectMenuItem value={technique.id} key={technique.id}>
Expand Down

0 comments on commit 498baff

Please sign in to comment.