npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

myfirst-color

v1.0.0

Published

基于 Vue 的颜色选择器

Downloads

1

Readme

vColorPicker 组件(带测试模块)

基于Vue的颜色选择器插件

安装

npm install

开发环境

npm run dev

快速打包

npm run build

打包并打印信息

npm run build --report

单元测试

npm run test

测试步骤

  1. 把vue组件放到 packages 目录下,文件目录结构参照color-picker
  2. 在 examples/App.vue 引用该组件
  3. 在 test/unit/specs/ 目录下新建文件 [组件名].spec.js,编写测试用例(可参照已有测试用例)
  4. 执行 npm run test
  5. 执行完毕会在 test/unit 下生成 coverage 文件夹,点开 coverage/Icov-report ,浏览器打开 index.html 可以看到图形界面的报告

注意

  1. 测试脚本都放在 test/unit/specs/ 目录下
  2. 脚本命名方式是[组件名].spec.js
  3. 单元测试默认测试 examples 目录下除了 main.js 之外的所有文件,可在 test/unit/index.js 文件中修改
  4. 测试脚本里面应该包括一个或多个describe块,每个describe块应该包括一个或多个it块。 describe块称为"测试套件"(test suite),表示一组相关的测试。它是一个函数,第一个参数是测试套件的名称("加法函数的测试"),第二个参数是一个实际执行的函数。 it块称为"测试用例"(test case),表示一个单独的测试,是测试的最小单位。它也是一个函数,第一个参数是测试用例的名称("1 加 1 应该等于 2"),第二个参数是一个实际执行的函数。

color-picker组件带了单元测试的几种类型

组件中DOM内容测试


// 校验dom结构内的文字内容
it('color-picker 初始化的内容与预期一致', () => {
  const Constructor = Vue.extend(colorPicker)
  const vm = new Constructor().$mount()
  expect(vm.$el.querySelector('.m-colorPicker h2').textContent)
  .to.equal('颜色选择器')
})

组件中data变量测试


it('setData()方法', () => {
  // 挂载组件
  const wrapper = mount(colorPicker);
  // setData
  wrapper.setData({ hoveColor: '#c21401' });
  // 断言:hoverColor值为#c21401
  expect(wrapper.vm.hoveColor).to.equal('#c21401');
})

组件中props变量测试


// 模拟传值
function getRenderedText (Component, propsDataMsg) {
  const Ctor = Vue.extend(Component)
  const vm = new Ctor({ propsData: propsDataMsg}).$mount()
  return vm.value
}
it('props传值#ff0000', () => {
  // 断言:传值后value值为#ff0000
  expect(getRenderedText(colorPicker, {
    value: '#ff0000'
  })).to.eql('#ff0000')
})

组件中的方法测试

点击事件


// 校验点击事件后,data的值
it('点击默认颜色', () => {
  // 挂载组件
  const wrapper = mount(colorPicker);
  // 获取点击dom
  const button = wrapper.find('.defaultColor');
  // 模拟点击一次
  button.trigger('click');
  // 断言:defaultColor值为#000000
  expect(wrapper.vm.defaultColor).to.equal('#000000')
})

it('点击页面其他地方,弹窗关闭', () => {
  // 挂载组件
  const wrapper = mount(colorPicker);
  //新建点击事件
  const clickEvent = new window.Event('click');
  // 触发点击事件
  window.document.dispatchEvent(clickEvent)
  // 断言:openStatus值为false
  expect(wrapper.vm.openStatus).to.equal(false)
})

单元测试应该测试哪些内容

一个组件,dom展现的内容是通过data来获得的。 而data是可以通过methods被改变

要测试的内容

  1. 测试去修改data,看看dom是否对应变化,或者一些依赖变量是否变化
  2. 测试触发methods里面的函数,看看data是否改变,dom内容是否改变。

因实际业务开发限制,可以不对所有的data内容做测试用例,可以针对关键性功能,添加测试用例。

用例生成

color-picker.vue
    √ color-picker 初始化的内容与预期一致
    √ 未使用Vue-test-utils: 正确渲染h2的文字为颜色选择器
    √ 使用Vue-test-Utils: 正确渲染h2的文字为颜色选择器
    √ setData()方法
    √ props传值后value为#ff0000
    √ 点击更多颜色
    √ 点击默认颜色
    √ 点击页面其他地方,弹窗关闭

Chrome 73.0.3683 (Windows 7 0.0.0): Executed 9 of 9 SUCCESS (0.55 secs / 0.369 secs)
TOTAL: 9 SUCCESS


=============================== Coverage summary ===============================
Statements   : 94.87% ( 37/39 )
Branches     : 83.33% ( 5/6 )
Functions    : 100% ( 1/1 )
Lines        : 94.87% ( 37/39 )
================================================================================

更多测试信息

了解更多,请参考 vue-test-utils, mocha, 以及测试覆盖率

打包发布你的组件到 npm 仓库

  1. 在package.json文件中,修改name、version、description、repository。在npm仓库中不能有相同的name
  2. 在发布之前首先要有npm账号 -> 注册npm账号
  3. 本地登录,执行:npm login,查看是否登录成功:npm whoami
  4. 发布组件:npm publish
  5. 撤销发布:npm unpublish 组件名 --force

更多有关npm发布

请参考 vue组件发布npm最佳实践, 封装Vue组件并使用npm发布

安装

$ npm install vcolorpicker -S

使用

main.js 文件中引入插件并注册

# main.js
import vcolorpicker from 'vcolorpicker'
Vue.use(vcolorpicker)

在项目中使用 vcolorpicker

<template>
  <colorPicker v-model="color" />
</template>
<script>
  export default {
    data () {
      return {
        color: '#ff0000'
      }
    }
  }
</script>

特点

  1. 简单易用,UI在原插件基础上优化增加了圆角和过渡动画
  2. 提供以 npm 的形式安装提供全局组件
  3. 在支持 html5 input[type='color'] 的浏览器实现了「更多颜色」的功能

选项

你可以通过在所在的元素上设置以下属性来配置color-picker

  1. defaultColor:默认颜色,如defaultColor="#ff0000"
  2. disabled:禁用状态,如disabled=true

事件

change颜色值改变的时候触发

<colorPicker v-model="color" v-on:change="headleChangeColor" />