Skip to content
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

test(divider): add unit test #771

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
plugins: [
['@babel/plugin-proposal-decorators', { 'legacy': true }],
['@babel/plugin-proposal-class-properties', { 'loose' : true }]
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
],
};
13 changes: 8 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ module.exports = {
coverageDirectory: '<rootDir>/test/unit/coverage',
reporters: [
'default',
['./node_modules/jest-html-reporter', {
pageTitle: 'TDesign-miniprogram Unit Test Report',
outputPath: './test/unit/report/test-report.html',
}],
[
'./node_modules/jest-html-reporter',
{
pageTitle: 'TDesign-miniprogram Unit Test Report',
outputPath: './test/unit/report/test-report.html',
},
],
],
setupFiles: ['<rootDir>/script/test/setup.js'],
coverageReporters: ['html', 'json-summary'],
globals: {
CONFIG_PREFIX: 't',
},
snapshotSerializers: ["miniprogram-simulate/jest-snapshot-plugin"]
snapshotSerializers: ['miniprogram-simulate/jest-snapshot-plugin'],
};
1 change: 1 addition & 0 deletions src/divider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ spline: message
isComponent: true
---

<span class="coverages-badge" style="margin-right: 10px"><img src="https://img.shields.io/badge/coverages%3A%20lines-100%25-blue" /></span><span class="coverages-badge" style="margin-right: 10px"><img src="https://img.shields.io/badge/coverages%3A%20functions-0%25-red" /></span><span class="coverages-badge" style="margin-right: 10px"><img src="https://img.shields.io/badge/coverages%3A%20statements-100%25-blue" /></span><span class="coverages-badge" style="margin-right: 10px"><img src="https://img.shields.io/badge/coverages%3A%20branches-0%25-red" /></span>
## 引入

全局引入,在 miniprogram 根目录下的`app.json`中配置,局部引入,在需要引入的页面或组件的`index.json`中配置。
Expand Down
119 changes: 119 additions & 0 deletions src/divider/__test__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import simulate from 'miniprogram-simulate';
import path from 'path';

// 文本位置
const textAlign = ['left', 'center', 'right'];

// 类型
const layout = ['horizontal', 'vertical'];

describe('Divider', () => {
const divider = simulate.load(path.resolve(__dirname, `../divider`), 't-divider', {
less: true,
rootPath: path.resolve(__dirname, '../..'),
});

textAlign.forEach((align) => {
it(`:align ${align} render correctly`, () => {
const id = simulate.load({
template: `<t-divider class="divider" align={{align}}></t-divider>`,
usingComponents: {
't-divider': divider,
},
data: {
align,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

const $divider = comp.querySelector('.divider >>> .t-divider');
expect($divider.dom.getAttribute('class').includes(`t-divider--${align}`));
});
});

it(`:content string render correctly`, () => {
const id = simulate.load({
template: `<t-divider class="divider" content={{content}}></t-divider>`,
usingComponents: {
't-divider': divider,
},
data: {
content: '文字信息',
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

const $divider = comp.querySelector('.divider >>> .t-divider__content');
expect($divider.dom.innerHTML).toBe('<wx-view> 文字信息 </wx-view>');
});

it(`:content slot render correctly`, () => {
const id = simulate.load({
template: `<t-divider class="divider"><text slot="content">文字信息</text></t-divider>`,
usingComponents: {
't-divider': divider,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

const $divider = comp.querySelector('.divider >>> .t-divider__content');
expect($divider.dom.innerHTML).toBe('<wx-text>文字信息</wx-text>');
});

it(':dashed render correctly', () => {
const id = simulate.load({
template: `<t-divider class="divider" dashed={{dashed}}></t-divider>`,
usingComponents: {
't-divider': divider,
},
data: {
dashed: true,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

const $divider = comp.querySelector('.divider >>> .t-divider');
expect($divider.dom.getAttribute('class').includes(`t-divider--dash`));
});

layout.forEach((lay) => {
it(`:layout ${lay} render correctly`, () => {
const id = simulate.load({
template: `<t-divider class="divider" layout={{layout}}></t-divider>`,
data: {
layout: 'horizontal',
},
usingComponents: {
't-divider': divider,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

const $divider = comp.querySelector('.divider >>> .t-divider');
expect($divider.dom.getAttribute('class').includes(`t-divider--${lay}`));
});
});

it(':line-color render correctly', () => {
const lineColor = '#eee';
const id = simulate.load({
template: `<t-divider class="divider" lineColor={{lineColor}}></t-divider>`,
usingComponents: {
't-divider': divider,
},
data: {
lineColor,
},
});
const comp = simulate.render(id);
comp.attach(document.createElement('parent-wrapper'));

const $divider = comp.querySelector('.divider >>> .t-divider');
expect($divider.dom.style.borderColor).toBe('#eee');
});
});