-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for default values in single choice questions in survey
Default values weren't selected automatically in surveys that showed a single option. With this change, a new `defaultOption` is added to the survey and used in case the selected value is not there. Now this works as it should, and how it works in Android. The reason why a default option was added and the default value is not just set as the selected one, is because the question view starts with props, and the prop is either selected or not. However, the question view wouldn't know if an option is selected because of user input or because of the default value. If the view is selected because of user input, then `opt.select(opt)` needs to be executed only on user interaction, but in case of default value, it needs to be run automatically as soon as the view is presented. I don't know how to make this distinction without having the default value separately. Also, a snapshot test was added to ensure that when there's a default value, it's selected correctly. MOB-2747
- Loading branch information
1 parent
6777e53
commit df37271
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
GliaWidgetsTests/Sources/Survey.SingleChouceQuestionViewTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import GliaWidgets | ||
|
||
final class SurveySingleChoiceQuestionViewTests: XCTestCase { | ||
static let firstOption = Survey.Option(name: "Option 1", value: "1") | ||
static let secondOption = Survey.Option(name: "Option 2", value: "2") | ||
|
||
var props: Survey.SingleChoiceQuestionView.Props = { | ||
var props = Survey.SingleChoiceQuestionView.Props( | ||
id: "1", | ||
title: "Survey", | ||
isRequired: true, | ||
accessibility: .init(value: "") | ||
) | ||
props.options = [ | ||
firstOption, | ||
secondOption | ||
] | ||
return props | ||
}() | ||
|
||
func test_regularOptionIsActive() { | ||
let regularOption = Self.firstOption | ||
|
||
let selection = Survey.SingleChoiceQuestionView.handleSelection( | ||
with: props, | ||
option: regularOption | ||
) | ||
|
||
XCTAssertEqual(selection, .active) | ||
} | ||
|
||
func test_defaultOptionIsSelected() { | ||
var hasSelectedDefaultOption = false | ||
|
||
let defaultOption = Survey.Option<String>( | ||
name: "Option 1", | ||
value: "1", | ||
select: { _ in | ||
hasSelectedDefaultOption = true | ||
} | ||
) | ||
|
||
props.defaultOption = defaultOption | ||
props.options = [defaultOption, Self.secondOption] | ||
|
||
let selection = Survey.SingleChoiceQuestionView.handleSelection( | ||
with: props, | ||
option: defaultOption | ||
) | ||
|
||
XCTAssertEqual(selection, .selected) | ||
XCTAssertEqual(hasSelectedDefaultOption, true) | ||
} | ||
|
||
func test_regularOptionIsActiveWhenDefaultIsPresent() { | ||
let defaultOption = Self.firstOption | ||
props.defaultOption = defaultOption | ||
|
||
let selection = Survey.SingleChoiceQuestionView.handleSelection( | ||
with: props, | ||
option: Self.secondOption | ||
) | ||
|
||
XCTAssertEqual(selection, .active) | ||
} | ||
|
||
func test_selectedOptionIsSelected() { | ||
let selectedOption = Self.firstOption | ||
props.selected = selectedOption | ||
|
||
let selection = Survey.SingleChoiceQuestionView.handleSelection( | ||
with: props, | ||
option: selectedOption | ||
) | ||
|
||
XCTAssertEqual(selection, .selected) | ||
} | ||
|
||
func test_regularOptionIsActiveWhenSelectedIsPresent() { | ||
let selectedOption = Self.firstOption | ||
props.selected = selectedOption | ||
|
||
let selection = Survey.SingleChoiceQuestionView.handleSelection( | ||
with: props, | ||
option: Self.secondOption | ||
) | ||
|
||
XCTAssertEqual(selection, .active) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters