Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HS-1245681] Hiding child question blocks #859

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/scripts/controllers/editRegistrationModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ angular
$scope.blockIsVisible = function (block, registrant) {
return (
block.type !== 'paragraphContent' &&
validateRegistrant.blockVisible(block, registrant, true)
validateRegistrant.blockVisible(
block,
registrant,
true,
$scope.conference,
)
);
};

Expand Down
7 changes: 6 additions & 1 deletion app/scripts/controllers/eventRegistrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,12 @@
};

$scope.blockIsVisible = function (block, registrant) {
return validateRegistrant.blockVisible(block, registrant, true);
return validateRegistrant.blockVisible(

Check warning on line 205 in app/scripts/controllers/eventRegistrations.js

View check run for this annotation

Codecov / codecov/patch

app/scripts/controllers/eventRegistrations.js#L205

Added line #L205 was not covered by tests
block,
registrant,
true,
$scope.conference,
);
};

var findAnswer = function (registration, blockId) {
Expand Down
8 changes: 8 additions & 0 deletions app/scripts/controllers/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ angular
currentRegistration.registrants.find(
(r) => r.id === registrantId,
),
false,
$scope.conference,
),
).length > 0
);
Expand All @@ -83,6 +85,8 @@ angular
currentRegistration.registrants.find(
(r) => r.id === registrantId,
),
false,
$scope.conference,
),
).length > 0
);
Expand Down Expand Up @@ -113,6 +117,8 @@ angular
currentRegistration.registrants.find(
(r) => r.id === registrantId,
),
false,
$scope.conference,
),
).length > 0,
)[0];
Expand Down Expand Up @@ -350,6 +356,8 @@ angular
_.find($scope.currentRegistration.registrants, {
id: $scope.currentRegistrant,
}),
false,
conference,
);
}),
true,
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/controllers/registrationModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ angular
block.type !== 'paragraphContent' &&
block.profileType !== 'NAME' &&
block.profileType !== 'EMAIL' &&
validateRegistrant.blockVisible(block, registrant, true)
validateRegistrant.blockVisible(block, registrant, true, conference)
);
};
},
Expand Down
9 changes: 8 additions & 1 deletion app/scripts/controllers/reviewRegistration.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ angular
validateRegistrant.blockVisible(
block,
currentRegistration.registrants.find((r) => r.id === registrantId),
false,
$scope.conference,
),
);

Expand Down Expand Up @@ -250,7 +252,12 @@ angular
};

$scope.blockVisibleForRegistrant = function (block, registrant) {
return validateRegistrant.blockVisible(block, registrant);
return validateRegistrant.blockVisible(
block,
registrant,
false,
$scope.conference,
);
};

$scope.acceptedPaymentMethods = function () {
Expand Down
7 changes: 6 additions & 1 deletion app/scripts/directives/blockRegistration.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@
var registrant = _.find($scope.currentRegistration.registrants, {
id: $scope.currentRegistrant,
});
return validateRegistrant.blockVisible(block, registrant);
return validateRegistrant.blockVisible(

Check warning on line 199 in app/scripts/directives/blockRegistration.js

View check run for this annotation

Codecov / codecov/patch

app/scripts/directives/blockRegistration.js#L199

Added line #L199 was not covered by tests
block,
registrant,
false,
$scope.conference,
);
};

function clearAnswerIfOptionHidden(isVisible, block, $scope, choice) {
Expand Down
27 changes: 25 additions & 2 deletions app/scripts/services/validateRegistrant.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
.service(
'validateRegistrant',
function validateRegistrant($window, ruleTypeConstants, $filter) {
const blockVisibleRuleCheck = (block, registrant, ruleType) => {
const blockVisibleRuleCheck = (
block,
registrant,
ruleType,
conference,
) => {
const blocks = conference
? _.flatMap(conference.registrationPages, 'blocks')
: undefined;

let answers = registrant.answers;
let ruleOperand = '';
let validRuleCount = 0;
Expand Down Expand Up @@ -73,6 +82,19 @@
? parseFloat(rule.value)
: rule.value;
}
// Hide this block if the question that the rule is based on (parentBlock) is hidden.
let parentBlock = _.find(blocks, { id: rule.parentBlockId });
if (
conference &&
!blockVisibleRuleCheck(
parentBlock,
registrant,
ruleType,
conference,
)
) {
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
return;

Check warning on line 96 in app/scripts/services/validateRegistrant.js

View check run for this annotation

Codecov / codecov/patch

app/scripts/services/validateRegistrant.js#L96

Added line #L96 was not covered by tests
}

if (rule.operator === '=' && answerValue === ruleValue) {
validRuleCount++;
Expand Down Expand Up @@ -214,13 +236,14 @@
return false;
};

this.blockVisible = (block, registrant, isAdmin) => {
this.blockVisible = (block, registrant, isAdmin, conference) => {
const visible =
angular.isDefined(registrant) &&
blockVisibleRuleCheck(
block,
registrant,
ruleTypeConstants.SHOW_QUESTION,
conference,
) &&
blockInRegistrantType(block, registrant) &&
this.isAnyChoiceVisible(block, registrant);
Expand Down
10 changes: 5 additions & 5 deletions test/spec/controllers/eventForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ describe('Controller: eventForm', function () {

describe('addNewPage', () => {
it('adds a new page', () => {
expect(scope.conference.registrationPages.length).toBe(2);
expect(scope.conference.registrationPages.length).toBe(3);

scope.addNewPage();
const newPage = scope.conference.registrationPages[2];
const newPage = scope.conference.registrationPages[3];

expect(scope.conference.registrationPages.length).toBe(3);
expect(newPage.title).toBe('Page 3');
expect($location.hash()).toBe('page3');
expect(scope.conference.registrationPages.length).toBe(4);
expect(newPage.title).toBe('Page 4');
expect($location.hash()).toBe('page4');
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/spec/controllers/registration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Controller: registration', () => {
it('should have validPages based on current registrant', () => {
let validPages = scope.validPages;

expect(validPages.length).toEqual(2);
expect(validPages.length).toEqual(3);
});

it('should getFirstValidPage', () => {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Controller: registration', () => {
},
];

expect(scope.validPages.length).toEqual(2);
expect(scope.validPages.length).toEqual(3);
scope.currentRegistrant = testData.registration.registrants[1].id;
scope.checkValidPages();

Expand All @@ -126,7 +126,7 @@ describe('Controller: registration', () => {

expect(checkValidPagesSpy).toHaveBeenCalledWith();

expect(scope.validPages.length).toEqual(2);
expect(scope.validPages.length).toEqual(3);

expect(nextPage).toEqual(scope.conference.registrationPages[1]);
});
Expand Down
161 changes: 155 additions & 6 deletions test/spec/services/validateRegistrant.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('Service: validateRegistrant', function () {
validateRegistrant.blockVisible(
testData.conference.registrationPages[1].blocks[0],
testData.registration.registrants[0],
false,
testData.conference,
),
).toBe(true);
});
Expand Down Expand Up @@ -285,9 +287,14 @@ describe('Service: validateRegistrant', function () {
};

// at least one choice is visible
expect(validateRegistrant.blockVisible(block, registrant, false)).toBe(
true,
);
expect(
validateRegistrant.blockVisible(
block,
registrant,
false,
testData.conference,
),
).toBe(true);
});

it('no choices are visible, block should be hidden', function () {
Expand All @@ -306,8 +313,150 @@ describe('Service: validateRegistrant', function () {
};

// no choices are visible
expect(validateRegistrant.blockVisible(block, registrant, false)).toBe(
false,
);
expect(
validateRegistrant.blockVisible(
block,
registrant,
false,
testData.conference,
),
).toBe(false);
});

describe('blockVisible()', function () {
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
const blockIds = [
'd6f1b12a-8c98-4e83-8857-1111111',
'38f8ece0-adf7-423d-9588-2222222',
'a7acefb9-72ef-4195-9b14-3333333',
];
const Q1Multiple = _.find(testData.conference.registrationPages[2].blocks, {
id: blockIds[0],
});
const Q2Multiple = _.find(testData.conference.registrationPages[2].blocks, {
id: blockIds[1],
});
const Q3Dropdown = _.find(testData.conference.registrationPages[2].blocks, {
id: blockIds[2],
});
const registrant = angular.copy(testData.registration.registrants[0]);
const answerParent =
registrant.answers[
_.findIndex(registrant.answers, {
blockId: blockIds[0],
})
];
const answerChild =
registrant.answers[
_.findIndex(registrant.answers, {
blockId: blockIds[1],
})
];
const answerGrandchild =
registrant.answers[
_.findIndex(registrant.answers, {
blockId: blockIds[2],
})
];

it('should only show the parent question when its answer is empty', function () {
answerParent.value = '';
answerChild.value = '';
answerGrandchild.value = '';

expect(
validateRegistrant.blockVisible(
Q1Multiple,
registrant,
false,
testData.conference,
),
).toBe(true);

expect(
validateRegistrant.blockVisible(
Q2Multiple,
registrant,
false,
testData.conference,
),
).toBe(false);

expect(
validateRegistrant.blockVisible(
Q3Dropdown,
registrant,
false,
testData.conference,
),
).toBe(false);
});

it('should show child and grandchild questions when answers are selected', function () {
// all are visible
answerParent.value = 'radio option - show child';
answerChild.value = 'radio option - show grandchild';
answerGrandchild.value = '';

expect(
validateRegistrant.blockVisible(
Q1Multiple,
registrant,
false,
testData.conference,
),
).toBe(true);

expect(
validateRegistrant.blockVisible(
Q2Multiple,
registrant,
false,
testData.conference,
),
).toBe(true);

expect(
validateRegistrant.blockVisible(
Q3Dropdown,
registrant,
false,
testData.conference,
),
).toBe(true);
});

it('should hide the child and grandchild questions when the parent answer change and the child is hidden', function () {
// all are visible
answerParent.value = 'radio option - hide child';
answerChild.value = 'radio option - show grandchild';
answerGrandchild.value = '1';

expect(
validateRegistrant.blockVisible(
Q1Multiple,
registrant,
false,
testData.conference,
),
).toBe(true);

expect(
validateRegistrant.blockVisible(
Q2Multiple,
registrant,
false,
testData.conference,
),
).toBe(false);

expect(
validateRegistrant.blockVisible(
Q3Dropdown,
registrant,
false,
testData.conference,
),
).toBe(false);
});
});
});
Loading
Loading