-
Notifications
You must be signed in to change notification settings - Fork 74
Fix date payload type in ui-button
#2019
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add tests. Here are some I prepared ealier...
test/nodes/widgets/ui_button.spec.js
near the top (in scope)
/** utility to wait for an event and return a promise */
function on (emitter, event) {
return new Promise((resolve) => {
emitter.on(event, resolve)
})
}towards the end of the test file
it('should receive an iso date in payload', async function () {
const copyOfFlow = JSON.parse(JSON.stringify(flow))
const buttonDefinition = copyOfFlow.find(n => n.id === 'node-ui-button')
buttonDefinition.payload = 'iso'
buttonDefinition.payloadType = 'date'
await helper.load(nodeImports, copyOfFlow)
verifyFlowLoaded(helper, copyOfFlow)
const base = helper.getNode('config-ui-base')
// setup a fake connection so that button has somewhere to emit to and we can spy on it
const socket = {
id: 'fake-conn-id',
emit: sinon.spy()
}
base.uiShared.connections['fake-conn-id'] = socket // fake connection so that button has somewhere to emit
// send a message to the button node
const button = helper.getNode('node-ui-button')
const hNode = helper.getNode('helper-node')
const inputPromise = on(hNode, 'input')
button.receive({})
const msg = await inputPromise
// tests
socket.emit.calledOnce.should.be.true()
const [eventName, message] = socket.emit.args[0] // [0][0] is the event name, [0][1] is the payload
eventName.should.equal('msg-input:' + button.id)
message.should.equal(msg)
msg.should.have.property('payload').and.be.a.String()
new Date(msg.payload).getTime().should.be.approximately(Date.now(), 500) // within 0.5 second
})
it('should receive a date object in payload', async function () {
const copyOfFlow = JSON.parse(JSON.stringify(flow))
const buttonDefinition = copyOfFlow.find(n => n.id === 'node-ui-button')
buttonDefinition.payload = 'object'
buttonDefinition.payloadType = 'date'
await helper.load(nodeImports, copyOfFlow)
verifyFlowLoaded(helper, copyOfFlow)
const base = helper.getNode('config-ui-base')
// setup a fake connection so that button has somewhere to emit to and we can spy on it
const socket = {
id: 'fake-conn-id',
emit: sinon.spy()
}
base.uiShared.connections['fake-conn-id'] = socket // fake connection so that button has somewhere to emit
// send a message to the button node
const button = helper.getNode('node-ui-button')
const hNode = helper.getNode('helper-node')
const inputPromise = on(hNode, 'input')
button.receive({})
const msg = await inputPromise
// tests
socket.emit.calledOnce.should.be.true()
const [eventName, message] = socket.emit.args[0] // [0][0] is the event name, [0][1] is the payload
eventName.should.equal('msg-input:' + button.id)
message.should.equal(msg)
msg.should.have.property('payload').and.be.an.instanceOf(Date)
msg.payload.getTime().should.be.approximately(Date.now(), 500) // within 0.5 second
})
it('should receive an epoch time value in payload', async function () {
const copyOfFlow = JSON.parse(JSON.stringify(flow))
const buttonDefinition = copyOfFlow.find(n => n.id === 'node-ui-button')
buttonDefinition.payload = '' // should be blank for epoch time
buttonDefinition.payloadType = 'date'
await helper.load(nodeImports, copyOfFlow)
verifyFlowLoaded(helper, copyOfFlow)
const base = helper.getNode('config-ui-base')
// setup a fake connection so that button has somewhere to emit to and we can spy on it
const socket = {
id: 'fake-conn-id',
emit: sinon.spy()
}
base.uiShared.connections['fake-conn-id'] = socket // fake connection so that button has somewhere to emit
// send a message to the button node
const button = helper.getNode('node-ui-button')
const hNode = helper.getNode('helper-node')
const inputPromise = on(hNode, 'input')
button.receive({})
const msg = await inputPromise
// tests
socket.emit.calledOnce.should.be.true()
const [eventName, message] = socket.emit.args[0] // [0][0] is the event name, [0][1] is the payload
eventName.should.equal('msg-input:' + button.id)
message.should.equal(msg)
msg.should.have.property('payload').and.be.a.Number()
msg.payload.should.be.approximately(Date.now(), 500) // within 0.5 second
})| switch (payload) { | ||
| case 'iso': | ||
| payload = new Date().toISOString() | ||
| break | ||
| case 'object': | ||
| payload = new Date() | ||
| break | ||
| default: | ||
| payload = Date.now() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The date should be evaluated by the core node-red typedInput partner function evaluateNodeProperty (to cater for future core changes)
| switch (payload) { | |
| case 'iso': | |
| payload = new Date().toISOString() | |
| break | |
| case 'object': | |
| payload = new Date() | |
| break | |
| default: | |
| payload = Date.now() | |
| } | |
| payload = RED.util.evaluateNodeProperty(payload, payloadType, node) |
Description
Fix
ui-buttonpayload date format type selectionRelated Issue(s)
closes #2018
Checklist
flowforge.yml?FlowFuse/helmto update ConfigMap TemplateFlowFuse/CloudProjectto update values for Staging/ProductionLabels
area:migrationlabel