Skip to content

Commit

Permalink
test: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Oct 25, 2022
1 parent a5b8e3a commit 442ff26
Showing 1 changed file with 68 additions and 2 deletions.
70 changes: 68 additions & 2 deletions packages/wc/test/renderer/focusNode.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { testFocusNode } from '@shaperone/testing/models/form'
import { testFocusNode, testPropertyState, testObjectState } from '@shaperone/testing/models/form'
import { sinon } from '@shaperone/testing'
import { FormRenderer } from '@hydrofoil/shaperone-core/renderer'
import { FocusNode } from '@hydrofoil/shaperone-core'
import { fixture, html, expect } from '@open-wc/testing'
import { Dispatch } from '@hydrofoil/shaperone-core/state'
import { any } from '@shaperone/testing/nodeFactory'
import { any, blankNode } from '@shaperone/testing/nodeFactory'
import { formRenderer } from '@shaperone/testing/renderer'
import { renderFocusNode } from '../../renderer/focusNode'

Expand Down Expand Up @@ -66,4 +66,70 @@ describe('wc/renderer/focusNode', () => {
focusNode: childState,
}))
})

describe('actions', () => {
describe('clearProperty', () => {
it('calls remove for every object', async () => {
// given
const childState = testFocusNode(focusNode.blankNode())
const property = testPropertyState(blankNode(), {
objects: [testObjectState(), testObjectState(), testObjectState()],
})
childState.properties = [property]
renderer.context.state.focusNodes[focusNode.value] = childState
renderer.context.templates.focusNode = sinon.stub().callsFake(({ actions }) => html`<button @click="${() => {
actions.clearProperty(property.shape)
}}"></button>`)

// when
const result = await fixture<HTMLElement>(renderFocusNode.call(renderer, { focusNode }))
result.click()

// then
expect(dispatch.removeObject).to.have.been.called.callCount(3)
})

it('calls clearValue when property disallows removing object', async () => {
// given
const childState = testFocusNode(focusNode.blankNode())
const property = testPropertyState(blankNode(), {
objects: [testObjectState(), testObjectState(), testObjectState()],
canRemove: false,
})
childState.properties = [property]
renderer.context.state.focusNodes[focusNode.value] = childState
renderer.context.templates.focusNode = sinon.stub().callsFake(({ actions }) => html`<button @click="${() => {
actions.clearProperty(property.shape)
}}"></button>`)

// when
const result = await fixture<HTMLElement>(renderFocusNode.call(renderer, { focusNode }))
result.click()

// then
expect(dispatch.clearValue).to.have.been.called.callCount(3)
})

it('does nothing when property is not found', async () => {
// given
const childState = testFocusNode(focusNode.blankNode())
const property = testPropertyState(blankNode(), {
objects: [testObjectState(), testObjectState(), testObjectState()],
})
childState.properties = [property]
renderer.context.state.focusNodes[focusNode.value] = childState
renderer.context.templates.focusNode = sinon.stub().callsFake(({ actions }) => html`<button @click="${() => {
actions.clearProperty(testPropertyState().shape)
}}"></button>`)

// when
const result = await fixture<HTMLElement>(renderFocusNode.call(renderer, { focusNode }))
result.click()

// then
expect(dispatch.clearValue).not.to.have.been.called
expect(dispatch.removeObject).not.to.have.been.called
})
})
})
})

0 comments on commit 442ff26

Please sign in to comment.