diff --git a/packages/mpx-cube-ui/__tests__/components/switch/__snapshots__/switch.spec.js.snap b/packages/mpx-cube-ui/__tests__/components/switch/__snapshots__/switch.spec.js.snap new file mode 100644 index 0000000..f5cad2d --- /dev/null +++ b/packages/mpx-cube-ui/__tests__/components/switch/__snapshots__/switch.spec.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`component switch unit test correct render check matchSnapshot 1`] = `""`; + +exports[`component switch unit test disabled check matchSnapshot 1`] = `""`; + +exports[`component switch unit test render check render 1`] = `""`; + +exports[`component switch unit test render check render 2`] = `""`; + +exports[`component switch unit test render check render 3`] = `""`; + +exports[`component switch unit test render check render 4`] = `""`; + +exports[`component switch unit test render check render 5`] = `""`; + +exports[`component switch unit test slot check render correct 1`] = `"
"`; + +exports[`component switch unit test wx:model check matchSnapshot 1`] = `"默认值:false"`; diff --git a/packages/mpx-cube-ui/__tests__/components/switch/switch.spec.js b/packages/mpx-cube-ui/__tests__/components/switch/switch.spec.js new file mode 100644 index 0000000..396d1d9 --- /dev/null +++ b/packages/mpx-cube-ui/__tests__/components/switch/switch.spec.js @@ -0,0 +1,78 @@ +const simulate = require('@mpxjs/miniprogram-simulate') + +describe('component switch unit test', function () { + function newComponent(componentId, props) { + const component = simulate.render(componentId, props) + const parent = document.createElement('parent') + component.attach(parent) // 会触发 attach 生命周期 + return component + } + describe('wx:model check', () => { + const componentId = simulate.loadMpx( + 'test/components/switch/template/wx-model.mpx' + ) + const component = newComponent(componentId) + it('matchSnapshot', () => { + expect(component.dom.innerHTML).toMatchSnapshot() // 判断前后生成的dom是否一样 + }) + const switchComponent = component.querySelector('.cube-switch') + it('wx:model', async () => { + // 确保 switchValue 首次显示符合预期 + expect(component.instance.switchValue).toBe(false) + expect(switchComponent.instance.value).toBe(false) + + switchComponent.querySelector('.cube-switch').dispatchEvent('tap') + await simulate.sleep(10) + + // 点击开关后,状态改变为打开 + expect(switchComponent.instance.value).toBe(true) + expect(component.instance.switchValue).toBe(true) + + // 改变父组件的值,switch的值也会改变 + component.instance.switchValue = false + await simulate.sleep(10) + expect(component.instance.switchValue).toBe(false) + expect(switchComponent.instance.value).toBe(false) + }) + }) + + const componentId1 = simulate.loadMpx('src/components/switch/index.mpx') + describe('correct render check', () => { + const component = newComponent(componentId1, {}) + + it('matchSnapshot', () => { + expect(component.dom.innerHTML).toMatchSnapshot() // 判断前后生成的dom是否一样 + }) + + it('correct render check', async () => { + // 正确渲染 + const isCloseSwitch = component.querySelector('.cube-switch-on') + + // eslint-disable-next-line eqeqeq + expect(component.instance.disabled).toBe(false) + + // eslint-disable-next-line eqeqeq + expect(isCloseSwitch === undefined).toBe(true) + }) + }) + + describe('disabled check', () => { + const component = newComponent(componentId1, { + disabled: true + }) + it('matchSnapshot', () => { + expect(component.dom.innerHTML).toMatchSnapshot() // 判断前后生成的dom是否一样 + }) + + it('disabled check', async () => { + // 正确渲染 + const isCloseSwitchOne = component.querySelector('.cube-switch-on') + expect(isCloseSwitchOne === undefined).toBe(true) + component.querySelector('.cube-switch').dispatchEvent('tap') + await simulate.sleep(10) + + const isCloseSwitchTwo = component.querySelector('.cube-switch-on') + expect(isCloseSwitchTwo === undefined).toBe(true) + }) + }) +}) diff --git a/packages/mpx-cube-ui/__tests__/components/switch/template/wx-model.mpx b/packages/mpx-cube-ui/__tests__/components/switch/template/wx-model.mpx new file mode 100644 index 0000000..057d342 --- /dev/null +++ b/packages/mpx-cube-ui/__tests__/components/switch/template/wx-model.mpx @@ -0,0 +1,25 @@ + + + +【 + +