-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from jkuatdsc/develop
Develop
- Loading branch information
Showing
13 changed files
with
246 additions
and
30 deletions.
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
18 changes: 18 additions & 0 deletions
18
example/src/main/java/com/dsc/formbuilder/screens/survey/components/SurveyModel.kt
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,18 @@ | ||
package com.dsc.formbuilder.screens.survey.components | ||
|
||
data class SurveyModel( | ||
// First page | ||
val email: String, | ||
val number: String, | ||
val username: String, | ||
|
||
// Second page | ||
val ide: List<String>, | ||
val platform: List<String>, | ||
val language: List<String>, | ||
|
||
// Third page | ||
val os: String, | ||
val gender: String, | ||
val experience: String, | ||
) |
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
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
42 changes: 42 additions & 0 deletions
42
form-builder/src/test/java/com/dsc/form_builder/FormStateTest.kt
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,42 @@ | ||
package com.dsc.form_builder | ||
|
||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
|
||
internal class FormStateTest { | ||
@Nested | ||
inner class DescribingFormState { | ||
private val formState = FormState( | ||
listOf( | ||
TextFieldState(name = "email"), | ||
SelectState(name = "hobbies"), | ||
ChoiceState(name = "gender"), | ||
TextFieldState(name = "age", initial = "34") | ||
) | ||
) | ||
|
||
private val emailState = formState.getState<TextFieldState>("email") | ||
private val hobbyState = formState.getState<SelectState>("hobbies") | ||
private val genderState = formState.getState<ChoiceState>("gender") | ||
private val ageState = formState.getState<TextFieldState>("age") | ||
|
||
@Test | ||
fun `state should be reset to initial values`() { | ||
// Given a form state with values changed | ||
emailState.change("[email protected]") | ||
hobbyState.select("Running") | ||
genderState.change("male") | ||
ageState.change("56") | ||
|
||
// When the form.reset is requested | ||
formState.reset() | ||
|
||
// Then all values are reset to the original state | ||
assert(emailState.value == "" && !emailState.hasError) | ||
assert(hobbyState.value == mutableListOf<String>() && !hobbyState.hasError) | ||
assert(genderState.value == "" && !genderState.hasError) | ||
assert(ageState.value == "34" && !ageState.hasError) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
form-builder/src/test/java/com/dsc/form_builder/SelectStateProviders.kt
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,22 @@ | ||
package com.dsc.form_builder | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.ArgumentsProvider | ||
import java.util.stream.Stream | ||
|
||
object MinArgumentsProvider: ArgumentsProvider { | ||
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> = Stream.of( | ||
Arguments.of(mutableListOf("item 1"), 2, false), | ||
Arguments.of(mutableListOf("item 1", "item 2"), 2, true), | ||
Arguments.of(mutableListOf("item 1", "item 2", "item 3"), 2, true), | ||
) | ||
} | ||
|
||
object MaxArgumentsProvider: ArgumentsProvider { | ||
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> = Stream.of( | ||
Arguments.of(mutableListOf("item 1"), 2, true), | ||
Arguments.of(mutableListOf("item 1", "item 2"), 2, true), | ||
Arguments.of(mutableListOf("item 1", "item 2", "item 3"), 2, false), | ||
) | ||
} |
72 changes: 72 additions & 0 deletions
72
form-builder/src/test/java/com/dsc/form_builder/SelectStateTest.kt
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,72 @@ | ||
package com.dsc.form_builder | ||
|
||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ArgumentsSource | ||
|
||
internal class SelectStateTest { | ||
|
||
@Nested | ||
inner class DescribingStateChanges { | ||
private val classToTest: SelectState = SelectState(name = "test") | ||
|
||
@Test | ||
fun `errors should be hidden`() { | ||
// Simulate an existing validation error | ||
classToTest.hasError = true | ||
classToTest.errorMessage = "error message" | ||
|
||
val newValue = "new item" // Given a TextFieldState and a new value | ||
classToTest.select(newValue) // When change is called | ||
|
||
assert(!classToTest.hasError) | ||
assert(classToTest.errorMessage.isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `state should be updated`() { | ||
val item = "new item" // Given a TextFieldState and a new value | ||
classToTest.select(item) // When change is called | ||
|
||
assert(classToTest.value.contains(item)) // Then state should have the item | ||
|
||
classToTest.unselect(item) | ||
assert(!classToTest.value.contains(item)) // Then state should NOT have the item | ||
} | ||
} | ||
|
||
@Nested | ||
inner class DescribingValidation { | ||
private val classToTest: SelectState = SelectState(name = "test") | ||
|
||
@Test | ||
fun `Validators_Required works correctly`(){ | ||
// When state is empty | ||
val firstValidation = classToTest.validateRequired("should fail") | ||
assert(!firstValidation) | ||
|
||
classToTest.select("item") | ||
val secondValidation = classToTest.validateRequired("should pass") | ||
assert(secondValidation) | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(MinArgumentsProvider::class) | ||
fun `Validators_Min works correctly`(value: MutableList<String>, min: Int, expected: Boolean){ | ||
classToTest.value = value // set the field state | ||
|
||
val actual = classToTest.validateMin(min, "expected validation: $expected") | ||
assert(actual == expected) | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(MaxArgumentsProvider::class) | ||
fun `Validators_Max works correctly`(value: MutableList<String>, max: Int, expected: Boolean){ | ||
classToTest.value = value // set the field state | ||
|
||
val actual = classToTest.validateMax(max, "expected validation: $expected") | ||
assert(actual == expected) | ||
} | ||
} | ||
} |
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