Skip to content

Commit

Permalink
fix: Enabled setter to have null as param value (#122)
Browse files Browse the repository at this point in the history
* fix: Enabled setter to have null as param value
  • Loading branch information
kevinshahfws authored Aug 8, 2023
1 parent 0c4fbd6 commit 2bfee25
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
6 changes: 3 additions & 3 deletions languages/javascript/src/shared/Prop/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Transport from "../Transport/index.mjs"
import Events from "../Events/index.mjs"

function prop(moduleName, key, params, callbackOrValue = null, immutable, readonly, contextParameterCount) {
function prop(moduleName, key, params, callbackOrValue = undefined, immutable, readonly, contextParameterCount) {
const numArgs = Object.values(params).length

if (numArgs === contextParameterCount && callbackOrValue === null) {
if (numArgs === contextParameterCount && callbackOrValue === undefined) {
// getter
return Transport.send(moduleName, key, params)
} else if (numArgs === contextParameterCount && typeof callbackOrValue === 'function') {
Expand All @@ -13,7 +13,7 @@ function prop(moduleName, key, params, callbackOrValue = null, immutable, readon
throw new Error('Cannot subscribe to an immutable property')
}
return Events.listen(moduleName, key + 'Changed', ...Object.values(params), callbackOrValue)
} else if (numArgs === (contextParameterCount) && callbackOrValue !== null) {
} else if (numArgs === (contextParameterCount) && callbackOrValue !== undefined) {
// setter
if (immutable) {
throw new Error('Cannot set a value to an immutable property')
Expand Down
17 changes: 16 additions & 1 deletion test/openrpc/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
],
"properties": {
"foo": {
"type": "string",
"type": ["string", "null"],
"description": "A required field in the result."
},
"bar": {
Expand Down Expand Up @@ -133,6 +133,21 @@
"foo": "here's bar"
}
}
},
{
"name": "Example to set null value",
"params": [
{
"name": "parameter",
"value": null
}
],
"result": {
"name": "Another Result",
"value": {
"foo": null
}
}
}
]
},
Expand Down
18 changes: 13 additions & 5 deletions test/suite/properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,12 @@ beforeAll( () => {
}
else if (json.method === 'simple.setProperty') {
propertySetterWasTriggered = true
if (json.params.value.foo === 'a new foo!') {
if (json.params.value.foo === 'a new foo!' || json.params.value.foo === null) {
propertySetterWasTriggeredWithValue = true
}
}
})

Simple.property({
foo: 'a new foo!'
})

return new Promise( (resolve, reject) => {
setTimeout(resolve, 100)
})
Expand All @@ -76,6 +72,18 @@ test('Basic Property subscribe', () => {
});

test('Basic Property set', () => {
Simple.property({
foo: 'a new foo!'
})

expect(propertySetterWasTriggered).toBe(true)
expect(propertySetterWasTriggeredWithValue).toBe(true)
});

test('Basic Property set with null', () => {
Simple.property({
foo: null
})
expect(propertySetterWasTriggered).toBe(true)
expect(propertySetterWasTriggeredWithValue).toBe(true)
});

0 comments on commit 2bfee25

Please sign in to comment.