| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- import {
- compileAndStringify,
- getRoot,
- fireEvent,
- createInstance
- } from '../../helpers/index'
- function compileSnippet (snippet, additional) {
- const { render, staticRenderFns } = compileAndStringify(`<div>${snippet}</div>`)
- const id = String(Date.now() * Math.random())
- const instance = createInstance(id, `
- new Vue({
- el: 'body',
- render: ${render},
- staticRenderFns: ${staticRenderFns},
- ${additional}
- })
- `)
- return getRoot(instance).children[0]
- }
- describe('richtext component', () => {
- it('with no child', () => {
- expect(compileSnippet(`
- <richtext></richtext>
- `)).toEqual({
- type: 'richtext'
- })
- })
- it('with single text node', () => {
- expect(compileSnippet(`
- <richtext>single</richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: {
- value: 'single'
- }
- }]
- }
- })
- })
- describe('span', () => {
- it('single node', () => {
- expect(compileSnippet(`
- <richtext>
- <span>single</span>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: {
- value: 'single'
- }
- }]
- }
- })
- })
- it('multiple node', () => {
- expect(compileSnippet(`
- <richtext>
- <span>AAA</span>
- <span>BBB</span>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'AAA' }
- }, {
- type: 'span',
- attr: { value: 'BBB' }
- }]
- }
- })
- })
- it('with raw text', () => {
- expect(compileSnippet(`
- <richtext>
- AAA
- <span>BBB</span>CCC
- <span>DDD</span>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'AAA' }
- }, {
- type: 'span',
- attr: { value: 'BBB' }
- }, {
- type: 'span',
- attr: { value: 'CCC' }
- }, {
- type: 'span',
- attr: { value: 'DDD' }
- }]
- }
- })
- })
- })
- describe('a', () => {
- it('single node', () => {
- expect(compileSnippet(`
- <richtext>
- <a href="http://whatever.com"></a>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'a',
- attr: { href: 'http://whatever.com' }
- }]
- }
- })
- })
- it('multiple node', () => {
- expect(compileSnippet(`
- <richtext>
- <a href="http://a.whatever.com"></a>
- <a href="http://b.whatever.com"></a>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'a',
- attr: { href: 'http://a.whatever.com' }
- }, {
- type: 'a',
- attr: { href: 'http://b.whatever.com' }
- }]
- }
- })
- })
- })
- describe('image', () => {
- it('single node', () => {
- expect(compileSnippet(`
- <richtext>
- <image src="path/to/profile.png"></image>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'image',
- attr: { src: 'path/to/profile.png' }
- }]
- }
- })
- })
- it('multiple node', () => {
- expect(compileSnippet(`
- <richtext>
- <image src="path/to/A.png"></image>
- <image src="path/to/B.png"></image>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'image',
- attr: { src: 'path/to/A.png' }
- }, {
- type: 'image',
- attr: { src: 'path/to/B.png' }
- }]
- }
- })
- })
- it('with width and height', () => {
- expect(compileSnippet(`
- <richtext>
- <image
- style="width:150px;height:150px;"
- src="path/to/profile.png">
- </image>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'image',
- style: { width: '150px', height: '150px' },
- attr: { src: 'path/to/profile.png' }
- }]
- }
- })
- })
- })
- describe('nested', () => {
- it('span', () => {
- expect(compileSnippet(`
- <richtext>
- <span>AAA
- <span>
- <span>BBB</span>
- <span><span>CCC</span>DDD</span>
- </span>
- </span>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- children: [{
- type: 'span',
- attr: { value: 'AAA' }
- }, {
- type: 'span',
- children: [{
- type: 'span',
- attr: { value: 'BBB' }
- }, {
- type: 'span',
- children: [{
- type: 'span',
- attr: { value: 'CCC' }
- }, {
- type: 'span',
- attr: { value: 'DDD' }
- }]
- }]
- }]
- }]
- }
- })
- })
- it('image and a', () => {
- expect(compileSnippet(`
- <richtext>
- <span>title</span>
- <a href="http://remote.com/xx.js">
- <span><span>name</span></span>
- <image src="path/to/yy.gif"></image>
- </a>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'title' }
- }, {
- type: 'a',
- attr: { href: 'http://remote.com/xx.js' },
- children: [{
- type: 'span',
- children: [{
- type: 'span',
- attr: { value: 'name' }
- }]
- }, {
- type: 'image',
- attr: { src: 'path/to/yy.gif' }
- }]
- }]
- }
- })
- })
- })
- describe('with styles', () => {
- it('inline', () => {
- expect(compileSnippet(`
- <richtext>
- <span style="font-size:16px;color:#FF6600;">ABCD</span>
- <image style="width:33.33px;height:66.67px" src="path/to/A.png"></image>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- style: { fontSize: '16px', color: '#FF6600' },
- attr: { value: 'ABCD' }
- }, {
- type: 'image',
- style: { width: '33.33px', height: '66.67px' },
- attr: { src: 'path/to/A.png' }
- }]
- }
- })
- })
- it('class list', () => {
- expect(compileSnippet(`
- <richtext>
- <image class="icon" src="path/to/A.png"></image>
- <span class="title large">ABCD</span>
- </richtext>
- `, `
- style: {
- title: { color: '#FF6600' },
- large: { fontSize: 24 },
- icon: { width: 40, height: 60 }
- }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'image',
- style: { width: 40, height: 60 },
- attr: { src: 'path/to/A.png' }
- }, {
- type: 'span',
- style: { fontSize: 24, color: '#FF6600' },
- attr: { value: 'ABCD' }
- }]
- }
- })
- })
- })
- describe('data binding', () => {
- it('simple', () => {
- expect(compileSnippet(`
- <richtext>
- <span>{{name}}</span>
- </richtext>
- `, `data: { name: 'ABCDEFG' }`)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'ABCDEFG' }
- }]
- }
- })
- })
- it('nested', () => {
- expect(compileSnippet(`
- <richtext>
- <span>{{a}}</span>
- <span>{{b}}<span>{{c.d}}</span></span>
- <span>{{e}}</span>
- </richtext>
- `, `
- data: { a: 'A', b: 'B', c: { d: 'CD' }, e: 'E' }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'A' }
- }, {
- type: 'span',
- children: [{
- type: 'span',
- attr: { value: 'B' }
- }, {
- type: 'span',
- attr: { value: 'CD' }
- }]
- }, {
- type: 'span',
- attr: { value: 'E' }
- }]
- }
- })
- })
- it('update', () => {
- expect(compileSnippet(`
- <richtext>
- <span>{{name}}</span>
- </richtext>
- `, `
- data: { name: 'default' },
- created: function () {
- this.name = 'updated'
- }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'updated' }
- }]
- }
- })
- })
- it('attribute', () => {
- expect(compileSnippet(`
- <richtext>
- <span :label="label">{{name}}</span>
- </richtext>
- `, `
- data: {
- label: 'uid',
- name: '10100'
- }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: {
- label: 'uid',
- value: '10100'
- }
- }]
- }
- })
- })
- it('update attribute', () => {
- expect(compileSnippet(`
- <richtext>
- <span :label="label">{{name}}</span>
- </richtext>
- `, `
- data: {
- label: 'name',
- name: 'Hanks'
- },
- created: function () {
- this.label = 'uid';
- this.name = '10100';
- }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- attr: {
- label: 'uid',
- value: '10100'
- }
- }]
- }
- })
- })
- it('inline style', () => {
- expect(compileSnippet(`
- <richtext>
- <span :style="styleObject">ABCD</span>
- <span :style="{ textAlign: align, color: 'red' }">EFGH</span>
- </richtext>
- `, `
- data: {
- styleObject: { fontSize: '32px', color: '#F6F660' },
- align: 'center'
- }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- style: { fontSize: '32px', color: '#F6F660' },
- attr: { value: 'ABCD' }
- }, {
- type: 'span',
- style: { textAlign: 'center', color: 'red' },
- attr: { value: 'EFGH' }
- }]
- }
- })
- })
- it('class list', () => {
- expect(compileSnippet(`
- <richtext>
- <image :class="classList" src="path/to/A.png"></image>
- <span :class="['title', size]">ABCD</span>
- <span class="large" style="color:#F6F0F4">EFGH</span>
- </richtext>
- `, `
- style: {
- title: { color: '#FF6600' },
- large: { fontSize: 24 },
- icon: { width: 40, height: 60 }
- },
- data: {
- classList: ['unknown'],
- size: 'small'
- },
- created: function () {
- this.classList = ['icon'];
- this.size = 'large';
- }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'image',
- style: { width: 40, height: 60 },
- attr: { src: 'path/to/A.png' }
- }, {
- type: 'span',
- style: { fontSize: 24, color: '#FF6600' },
- attr: { value: 'ABCD' }
- }, {
- type: 'span',
- style: { fontSize: 24, color: '#F6F0F4' },
- attr: { value: 'EFGH' }
- }]
- }
- })
- })
- it('update inline style', () => {
- expect(compileSnippet(`
- <richtext>
- <span :style="styleObject">ABCD</span>
- <span :style="{ textAlign: align, color: 'red' }">EFGH</span>
- </richtext>
- `, `
- data: {
- styleObject: { fontSize: '32px', color: '#F6F660' }
- },
- created: function () {
- this.styleObject = { fontSize: '24px', color: 'blue' }
- this.styleObject.color = '#ABCDEF'
- this.align = 'left'
- }
- `)).toEqual({
- type: 'richtext',
- attr: {
- value: [{
- type: 'span',
- style: { fontSize: '24px', color: '#ABCDEF' },
- attr: { value: 'ABCD' }
- }, {
- type: 'span',
- style: { textAlign: 'left', color: 'red' },
- attr: { value: 'EFGH' }
- }]
- }
- })
- })
- })
- describe('itself', () => {
- it('inline styles', () => {
- expect(compileSnippet(`
- <richtext style="background-color:red">
- <span>empty</span>
- </richtext>
- `)).toEqual({
- type: 'richtext',
- style: { backgroundColor: 'red' },
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'empty' }
- }]
- }
- })
- })
- it('class list', () => {
- expect(compileSnippet(`
- <richtext class="title">
- <span class="large">ABCD</span>
- </richtext>
- `, `
- style: {
- title: { backgroundColor: '#FF6600', height: 200 },
- large: { fontSize: 24 }
- }
- `)).toEqual({
- type: 'richtext',
- classList: ['title'],
- attr: {
- value: [{
- type: 'span',
- style: { fontSize: 24 },
- attr: { value: 'ABCD' }
- }]
- }
- })
- })
- it('update styles', () => {
- expect(compileSnippet(`
- <richtext :class="classList" :style="{ backgroundColor: color }">
- <span class="large">ABCD</span>
- </richtext>
- `, `
- data: { classList: ['unknow'], color: '#FF6600' },
- style: {
- title: { height: 200 },
- large: { fontSize: 24 }
- },
- created: function () {
- this.classList = ['title']
- }
- `)).toEqual({
- type: 'richtext',
- classList: ['title'],
- style: { backgroundColor: '#FF6600' },
- attr: {
- value: [{
- type: 'span',
- style: { fontSize: 24 },
- attr: { value: 'ABCD' }
- }]
- }
- })
- })
- it('bind events', (done) => {
- const { render, staticRenderFns } = compileAndStringify(`
- <div>
- <richtext @click="handler">
- <span>Label: {{label}}</span>
- </richtext>
- </div>
- `)
- const id = String(Date.now() * Math.random())
- const instance = createInstance(id, `
- new Vue({
- el: 'body',
- render: ${render},
- staticRenderFns: ${staticRenderFns},
- data: { label: 'AAA' },
- methods: {
- handler: function () {
- this.label = 'BBB'
- }
- }
- })
- `)
- const richtext = instance.document.body.children[0]
- fireEvent(instance, richtext.ref, 'click')
- setTimeout(() => {
- expect(getRoot(instance).children[0]).toEqual({
- type: 'richtext',
- event: ['click'],
- attr: {
- value: [{
- type: 'span',
- attr: { value: 'Label: BBB' }
- }]
- }
- })
- done()
- }, 0)
- })
- it('v-for', () => {
- expect(compileSnippet(`
- <div>
- <richtext v-for="k in labels">
- <span>{{k}}</span>
- </richtext>
- </div>
- `, `
- data: {
- labels: ['A', 'B', 'C']
- }
- `)).toEqual({
- type: 'div',
- children: [{
- type: 'richtext',
- attr: { value: [{ type: 'span', attr: { value: 'A' }}] }
- }, {
- type: 'richtext',
- attr: { value: [{ type: 'span', attr: { value: 'B' }}] }
- }, {
- type: 'richtext',
- attr: { value: [{ type: 'span', attr: { value: 'C' }}] }
- }]
- })
- })
- })
- })
|