From a007380e0df47b373c9121a4e93bdf9c2a80bc15 Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Fri, 5 Aug 2022 21:06:48 +0800 Subject: [PATCH 1/2] test(radio): cache --- .../__test__/__snapshots__/demo.test.js.snap | 11 --- src/radio-group/__test__/demo.test.js | 16 ---- src/radio-group/__test__/index.test.js | 53 ----------- src/radio-group/radio-group.ts | 2 +- .../__test__/__snapshots__/index.test.js.snap | 88 +++++++++++-------- src/radio/__test__/index.js | 3 + src/radio/__test__/index.json | 6 ++ src/radio/__test__/index.test.js | 46 ++++++---- src/radio/__test__/index.wxml | 3 + 9 files changed, 94 insertions(+), 134 deletions(-) delete mode 100644 src/radio-group/__test__/__snapshots__/demo.test.js.snap delete mode 100644 src/radio-group/__test__/demo.test.js delete mode 100644 src/radio-group/__test__/index.test.js create mode 100644 src/radio/__test__/index.js create mode 100644 src/radio/__test__/index.json create mode 100644 src/radio/__test__/index.wxml diff --git a/src/radio-group/__test__/__snapshots__/demo.test.js.snap b/src/radio-group/__test__/__snapshots__/demo.test.js.snap deleted file mode 100644 index 910c69603..000000000 --- a/src/radio-group/__test__/__snapshots__/demo.test.js.snap +++ /dev/null @@ -1,11 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`radio group demo demo should render correctly 1`] = ` -
- -
-`; diff --git a/src/radio-group/__test__/demo.test.js b/src/radio-group/__test__/demo.test.js deleted file mode 100644 index d5e83b217..000000000 --- a/src/radio-group/__test__/demo.test.js +++ /dev/null @@ -1,16 +0,0 @@ -import simulate from 'miniprogram-simulate'; -import getDemoPath from '../../../test/utils/getDemoPath'; - -describe('radio group demo', () => { - let id; - beforeAll(() => { - id = simulate.load(getDemoPath('radio', 'group'), { - less: true, - }); - }); - test('demo should render correctly', async () => { - const container = simulate.render(id); - container.attach(document.createElement('parent-wrapper')); - expect(container.toJSON()).toMatchSnapshot(); - }); -}); diff --git a/src/radio-group/__test__/index.test.js b/src/radio-group/__test__/index.test.js deleted file mode 100644 index 8ee695681..000000000 --- a/src/radio-group/__test__/index.test.js +++ /dev/null @@ -1,53 +0,0 @@ -import simulate from 'miniprogram-simulate'; -import getDemoPath from '../../../test/utils/getDemoPath'; - -describe('radio-group', () => { - let id; - beforeAll(() => { - id = simulate.load(getDemoPath('radio', 'group'), { - less: true, - }); - }); - test('value should update correctly', async () => { - const items = [ - { - name: 'radio1', - title: '单行标题', - bordered: true, - }, - { - name: 'radio2', - title: '双行标题,长文本自动换行,该选项的描述是一段很长的内容', - bordered: true, - }, - { - name: 'radio3', - title: '双行标题,长文本自动换行,该选项的描述是一段很长的内容', - label: '一段很长很长的内容文字,一段很长很长的内容文字,一段很长很长的内容文字', - bordered: false, - }, - ]; - const container = simulate.render(id, { - items, - value: 'radio1', - }); - container.attach(document.createElement('parent-wrapper')); - const radioGroup = container.querySelector('.radio-group'); - radioGroup.dispatchEvent('change', { - detail: { - name: 'radio2', - checked: true, - }, - }); - await simulate.sleep(10); - expect(container.data.currentValue).toBe('radio2'); - radioGroup.dispatchEvent('change', { - detail: { - name: 'radio3', - checked: true, - }, - }); - await simulate.sleep(10); - expect(container.data.currentValue).toBe('radio3'); - }); -}); diff --git a/src/radio-group/radio-group.ts b/src/radio-group/radio-group.ts index b915e56f2..e980ad8a6 100644 --- a/src/radio-group/radio-group.ts +++ b/src/radio-group/radio-group.ts @@ -58,7 +58,7 @@ export default class RadioGroup extends SuperComponent { methods = { getChilds() { let items = this.getRelationNodes('../radio/radio'); - if (!items.length) { + if (!items?.length) { items = this.selectAllComponents(`.${prefix}-radio-option`); } return items; diff --git a/src/radio/__test__/__snapshots__/index.test.js.snap b/src/radio/__test__/__snapshots__/index.test.js.snap index 0caa45984..e9f928dab 100644 --- a/src/radio/__test__/__snapshots__/index.test.js.snap +++ b/src/radio/__test__/__snapshots__/index.test.js.snap @@ -1,50 +1,62 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`radio comp should render correctly 1`] = ` - - + - - - - - - - + + + + + + + + + - - - + + + - + + + + + - - - + + `; diff --git a/src/radio/__test__/index.js b/src/radio/__test__/index.js new file mode 100644 index 000000000..3ea459073 --- /dev/null +++ b/src/radio/__test__/index.js @@ -0,0 +1,3 @@ +Component({ + data: {}, +}); diff --git a/src/radio/__test__/index.json b/src/radio/__test__/index.json new file mode 100644 index 000000000..59f25ec76 --- /dev/null +++ b/src/radio/__test__/index.json @@ -0,0 +1,6 @@ +{ + "usingComponents": { + "t-radio": "../radio", + "t-radio-group": "../../radio-group/radio-group" + } +} diff --git a/src/radio/__test__/index.test.js b/src/radio/__test__/index.test.js index 2770b537a..563d3b9f9 100644 --- a/src/radio/__test__/index.test.js +++ b/src/radio/__test__/index.test.js @@ -2,22 +2,38 @@ import path from 'path'; import simulate from 'miniprogram-simulate'; describe('radio', () => { - const id = simulate.load(path.resolve(__dirname, '../../radio/radio'), 't-radio', { - less: true, - rootPath: path.resolve(__dirname, '../../'), - }); + // test('comp should trigger change events', async () => { + // const container = simulate.render(radio); + // const comp = container.instance; + // comp.triggerEvent = jest.fn(); + // comp.doChange(); + // expect(comp.triggerEvent).toHaveBeenCalled(); + // }); + // const id = simulate.load(path.resolve(__dirname, `./index`), { less: true }); - test('comp should render correctly', async () => { - const container = simulate.render(id); - container.attach(document.createElement('parent-wrapper')); - expect(container.toJSON()).toMatchSnapshot(); - }); + describe('with group', () => { + it(`radio :base`, async () => { + const id = simulate.load({ + template: ` + + + + `, + usingComponents: { + 't-radio': './radio', + 't-radio-group': '../../radio-group/radio-group', + }, + rootPath: path.resolve(__dirname, '../..'), + compiler: 'official', + }); + const comp = simulate.render(id); + comp.attach(document.createElement('parent-wrapper')); + + // const group = comp.querySelector('.group') + const a = comp.querySelector('.a'); - test('comp should trigger change events', async () => { - const container = simulate.render(id); - const comp = container.instance; - comp.triggerEvent = jest.fn(); - comp.doChange(); - expect(comp.triggerEvent).toHaveBeenCalled(); + expect(a.data.checked).toBeTruthy(); + // expect(comp.toJSON()).toMatchSnapshot(); + }); }); }); diff --git a/src/radio/__test__/index.wxml b/src/radio/__test__/index.wxml new file mode 100644 index 000000000..250489917 --- /dev/null +++ b/src/radio/__test__/index.wxml @@ -0,0 +1,3 @@ + + + From 793dded1bd635a60fd2cc651f9d8b869675eb1ed Mon Sep 17 00:00:00 2001 From: leejimqiu Date: Mon, 8 Aug 2022 14:59:01 +0800 Subject: [PATCH 2/2] test(radio): complete unit test --- src/radio/README.md | 1 + .../__test__/__snapshots__/index.test.js.snap | 62 ---------- src/radio/__test__/index.js | 12 +- src/radio/__test__/index.test.js | 110 +++++++++++++++--- src/radio/__test__/index.wxml | 10 +- 5 files changed, 115 insertions(+), 80 deletions(-) delete mode 100644 src/radio/__test__/__snapshots__/index.test.js.snap diff --git a/src/radio/README.md b/src/radio/README.md index 1dfb8bd88..62d1cd128 100644 --- a/src/radio/README.md +++ b/src/radio/README.md @@ -5,6 +5,7 @@ spline: form isComponent: true --- + ## 引入 全局引入,在 miniprogram 根目录下的`app.json`中配置,局部引入,在需要引入的页面或组件的`index.json`中配置。 diff --git a/src/radio/__test__/__snapshots__/index.test.js.snap b/src/radio/__test__/__snapshots__/index.test.js.snap deleted file mode 100644 index e9f928dab..000000000 --- a/src/radio/__test__/__snapshots__/index.test.js.snap +++ /dev/null @@ -1,62 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`radio with group radio :base 1`] = ` -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
-`; diff --git a/src/radio/__test__/index.js b/src/radio/__test__/index.js index 3ea459073..e46438def 100644 --- a/src/radio/__test__/index.js +++ b/src/radio/__test__/index.js @@ -1,3 +1,13 @@ Component({ - data: {}, + data: { + value: '1', + options: ['a', 'b', { label: 'c', value: 'c' }], + }, + methods: { + onChange(e) { + const { value } = e.detail; + + this.setData({ value }); + }, + }, }); diff --git a/src/radio/__test__/index.test.js b/src/radio/__test__/index.test.js index 563d3b9f9..922af3ec8 100644 --- a/src/radio/__test__/index.test.js +++ b/src/radio/__test__/index.test.js @@ -9,23 +9,51 @@ describe('radio', () => { // comp.doChange(); // expect(comp.triggerEvent).toHaveBeenCalled(); // }); - // const id = simulate.load(path.resolve(__dirname, `./index`), { less: true }); + const id = simulate.load(path.resolve(__dirname, `./index`), { less: true }); + + it(':base', async () => { + const comp = simulate.render(id); + comp.attach(document.createElement('parent-wrapper')); + + const radio = comp.querySelector('.single'); + + expect(radio.data.checked).not.toBeTruthy(); + + radio.querySelector('.t-radio__icon').dispatchEvent('tap'); + await simulate.sleep(10); + + expect(radio.data.checked).toBeTruthy(); + }); + + it(':disabled', async () => { + const comp = simulate.render(id); + comp.attach(document.createElement('parent-wrapper')); + + const radio = comp.querySelector('.disable'); + + expect(radio.data.checked).not.toBeTruthy(); + + radio.querySelector('.t-radio__icon').dispatchEvent('tap'); + await simulate.sleep(10); + + expect(radio.data.checked).not.toBeTruthy(); + }); describe('with group', () => { - it(`radio :base`, async () => { - const id = simulate.load({ - template: ` - - - - `, - usingComponents: { - 't-radio': './radio', - 't-radio-group': '../../radio-group/radio-group', - }, - rootPath: path.resolve(__dirname, '../..'), - compiler: 'official', - }); + it(`:base`, async () => { + // const id = simulate.load({ + // template: ` + // + // + // + // `, + // usingComponents: { + // 't-radio': './radio', + // 't-radio-group': '../../radio-group/radio-group', + // }, + // rootPath: path.resolve(__dirname, '../..'), + // compiler: 'official', + // }); const comp = simulate.render(id); comp.attach(document.createElement('parent-wrapper')); @@ -33,7 +61,57 @@ describe('radio', () => { const a = comp.querySelector('.a'); expect(a.data.checked).toBeTruthy(); - // expect(comp.toJSON()).toMatchSnapshot(); + + const b = comp.querySelector('.b >>> .t-radio__icon'); + + b.dispatchEvent('tap'); + + await simulate.sleep(10); + + expect(comp.data.value).toBe('2'); + }); + + it(`:disabled content`, async () => { + const comp = simulate.render(id); + comp.attach(document.createElement('parent-wrapper')); + + const a = comp.querySelector('.a'); + + expect(a.data.checked).toBeTruthy(); + + const b = comp.querySelector('.b >>> .t-radio__icon'); + + b.dispatchEvent('tap'); + + await simulate.sleep(10); + + expect(comp.data.value).toBe('2'); + + const contentOfC = comp.querySelector('.c >>> .t-radio__content'); + contentOfC.dispatchEvent('tap'); + await simulate.sleep(10); + + expect(comp.data.value).toBe('2'); + + const iconOfC = comp.querySelector('.c >>> .t-radio__icon'); + iconOfC.dispatchEvent('tap'); + await simulate.sleep(10); + + expect(comp.data.value).toBe('3'); + }); + + it(':with options', () => { + const comp = simulate.render(id); + comp.attach(document.createElement('parent-wrapper')); + + const group = comp.querySelector('#optionsGroup'); + group.instance.handleRadioChange({ + target: { + dataset: { value: 'b' }, + }, + }); + + expect(group.data.value).toBe('b'); }); }); }); diff --git a/src/radio/__test__/index.wxml b/src/radio/__test__/index.wxml index 250489917..c843fd6c1 100644 --- a/src/radio/__test__/index.wxml +++ b/src/radio/__test__/index.wxml @@ -1,3 +1,11 @@ - + + + + + + + + +