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

Fix issue with missing default values #773

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion src/core/value_properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ export function defaultValueForDefinition(typeDefinition: ValueTypeDefinition):
return typeDefinition
}

export function hasCustomDefaultValueForDefinition(typeDefinition: ValueTypeDefinition): boolean {
const constant = parseValueTypeConstant(typeDefinition as ValueTypeConstant)
if (constant) return false;

const hasDefault = hasProperty(typeDefinition, "default")
if (hasDefault) return true;

const hasType = hasProperty(typeDefinition, "type")
if (hasType) return false;

return true;
}

function valueDescriptorForTokenAndTypeDefinition(payload: ValueTypeDefinitionPayload) {
const { token, typeDefinition } = payload

Expand All @@ -211,7 +224,7 @@ function valueDescriptorForTokenAndTypeDefinition(payload: ValueTypeDefinitionPa
return defaultValueForDefinition(typeDefinition)
},
get hasCustomDefaultValue() {
return parseValueTypeDefault(typeDefinition) !== undefined
return hasCustomDefaultValueForDefinition(typeDefinition)
},
reader: readers[type],
writer: writers[type] || writers.default,
Expand Down
31 changes: 31 additions & 0 deletions src/tests/controllers/default_value_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@ export class DefaultValueController extends Controller {
defaultBooleanTrue: { type: Boolean, default: true },
defaultBooleanFalse: { type: Boolean, default: false },
defaultBooleanOverride: true,
defaultBooleanWithTypeConstant: Boolean,
defaultBooleanWithTypeProperty: { type: Boolean },

defaultString: "",
defaultStringHello: { type: String, default: "Hello" },
defaultStringOverride: "Override me",
defaultStringWithTypeConstant: String,
defaultStringWithTypeProperty: { type: String },

defaultNumber: 0,
defaultNumberThousand: { type: Number, default: 1000 },
defaultNumberZero: { type: Number, default: 0 },
defaultNumberOverride: 9999,
defaultNumberWithTypeConstant: Number,
defaultNumberWithTypeProperty: { type: Number },

defaultArray: [],
defaultArrayFilled: { type: Array, default: [1, 2, 3] },
defaultArrayOverride: [9, 9, 9],
defaultArrayWithTypeConstant: Array,
defaultArrayWithTypeProperty: { type: Array },

defaultObject: {},
defaultObjectPerson: { type: Object, default: { name: "David" } },
defaultObjectOverride: { override: "me" },
defaultObjectWithTypeConstant: Object,
defaultObjectWithTypeProperty: { type: Object },
}

valueDescriptorMap!: ValueDescriptorMap
Expand All @@ -36,13 +46,21 @@ export class DefaultValueController extends Controller {
hasDefaultBooleanFalseValue!: boolean
defaultBooleanOverrideValue!: boolean
hasDefaultBooleanOverrideValue!: boolean
defaultBooleanWithTypeConstantValue!: boolean
hasDefaultBooleanWithTypeConstantValue!: boolean
defaultBooleanWithTypePropertyValue!: boolean
hasDefaultBooleanWithTypePropertyValue!: boolean

defaultStringValue!: string
hasDefaultStringValue!: boolean
defaultStringHelloValue!: string
hasDefaultStringHelloValue!: boolean
defaultStringOverrideValue!: string
hasDefaultStringOverrideValue!: boolean
defaultStringWithTypeConstantValue!: string
hasDefaultStringWithTypeConstantValue!: boolean
defaultStringWithTypePropertyValue!: string
hasDefaultStringWithTypePropertyValue!: boolean

defaultNumberValue!: number
hasDefaultNumberValue!: boolean
Expand All @@ -52,20 +70,33 @@ export class DefaultValueController extends Controller {
hasDefaultNumberZeroValue!: boolean
defaultNumberOverrideValue!: number
hasDefaultNumberOverrideValue!: boolean
defaultNumberWithTypeConstantValue!: number
hasDefaultNumberWithTypeConstantValue!: boolean
defaultNumberWithTypePropertyValue!: number
hasDefaultNumberWithTypePropertyValue!: boolean

defaultArrayValue!: any[]
hasDefaultArrayValue!: boolean
defaultArrayFilledValue!: { [key: string]: any }
hasDefaultArrayFilledValue!: boolean
defaultArrayOverrideValue!: { [key: string]: any }
hasDefaultArrayOverrideValue!: boolean
defaultArrayWithTypeConstantValue!: any[]
hasDefaultArrayWithTypeConstantValue!: boolean
defaultArrayWithTypePropertyValue!: any[]
hasDefaultArrayWithTypePropertyValue!: boolean

defaultObjectValue!: object
hasDefaultObjectValue!: boolean
defaultObjectPersonValue!: object
hasDefaultObjectPersonValue!: boolean
defaultObjectOverrideValue!: object
hasDefaultObjectOverrideValue!: boolean
defaultObjectWithTypeConstantValue!: object
hasDefaultObjectWithTypeConstantValue!: boolean
defaultObjectWithTypePropertyValue!: object
hasDefaultObjectWithTypePropertyValue!: boolean

lifecycleCallbacks: string[] = []

initialize() {
Expand Down
25 changes: 25 additions & 0 deletions src/tests/modules/core/default_value_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export default class DefaultValueTests extends ControllerTestCase(DefaultValueCo
this.assert.ok(this.controller.hasDefaultBooleanOverrideValue)
}

"test boolean with no default value"() {
this.assert.notOk(this.controller.hasDefaultBooleanWithTypeConstantValue)
this.assert.notOk(this.controller.hasDefaultBooleanWithTypePropertyValue)
}

// Strings

"test custom default string values"() {
Expand Down Expand Up @@ -76,6 +81,11 @@ export default class DefaultValueTests extends ControllerTestCase(DefaultValueCo
this.assert.ok(this.controller.hasDefaultStringOverrideValue)
}

"test string with no default value"() {
this.assert.notOk(this.controller.hasDefaultStringWithTypeConstantValue)
this.assert.notOk(this.controller.hasDefaultStringWithTypePropertyValue)
}

// Numbers

"test custom default number values"() {
Expand Down Expand Up @@ -110,6 +120,11 @@ export default class DefaultValueTests extends ControllerTestCase(DefaultValueCo
this.assert.ok(this.controller.hasDefaultNumberOverrideValue)
}

"test number with no default value"() {
this.assert.notOk(this.controller.hasDefaultNumberWithTypeConstantValue)
this.assert.notOk(this.controller.hasDefaultNumberWithTypePropertyValue)
}

// Arrays

"test custom default array values"() {
Expand Down Expand Up @@ -140,6 +155,11 @@ export default class DefaultValueTests extends ControllerTestCase(DefaultValueCo
this.assert.ok(this.controller.hasDefaultArrayOverrideValue)
}

"test array with no default value"() {
this.assert.notOk(this.controller.hasDefaultArrayWithTypeConstantValue)
this.assert.notOk(this.controller.hasDefaultArrayWithTypePropertyValue)
}

// Objects

"test custom default object values"() {
Expand Down Expand Up @@ -170,6 +190,11 @@ export default class DefaultValueTests extends ControllerTestCase(DefaultValueCo
this.assert.ok(this.controller.hasDefaultObjectOverrideValue)
}

"test object with no default value"() {
this.assert.notOk(this.controller.hasDefaultObjectWithTypeConstantValue)
this.assert.notOk(this.controller.hasDefaultObjectWithTypePropertyValue)
}

"test [name]ValueChanged callbacks fire after initialize and before connect"() {
this.assert.deepEqual(this.controller.lifecycleCallbacks, ["initialize", "defaultBooleanValueChanged", "connect"])
}
Expand Down