|
| 1 | +import DataSet from '@antv/data-set'; |
| 2 | +import { DataItem, wordCloud } from '../../../../src/utils/transform/word-cloud'; |
| 3 | + |
| 4 | +const { View } = DataSet; |
| 5 | +const options = { |
| 6 | + type: 'tag-cloud', |
| 7 | + fields: ['text', 'value'], |
| 8 | + fontSize: 10, |
| 9 | + fontWeight: 'bold', |
| 10 | + size: [800, 800], |
| 11 | + padding: 0, |
| 12 | + timeInterval: 5000, |
| 13 | + rotate: 90, |
| 14 | +}; |
| 15 | +const data = ['Hello', 'world', 'normally', 'you', 'want', 'more', 'words', 'than', 'this'].map((d) => { |
| 16 | + return { |
| 17 | + text: d, |
| 18 | + value: 5 + Math.random() * 10, |
| 19 | + test: 'haha', |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +describe('word-cloud', () => { |
| 24 | + it('with data-set', () => { |
| 25 | + const dv = new View(); |
| 26 | + dv.source(data).transform(options as any); |
| 27 | + // 由于生成的每个单词的 x,y 坐标是比较随机的,每次都不一样, |
| 28 | + // 所以为了测试通过,把 x,y 属性删除。 |
| 29 | + function removeXY(v) { |
| 30 | + delete v.x; |
| 31 | + delete v.y; |
| 32 | + } |
| 33 | + const result1 = wordCloud(data, options as any); |
| 34 | + const result2 = dv.rows; |
| 35 | + |
| 36 | + result1.forEach(removeXY); |
| 37 | + result2.forEach(removeXY); |
| 38 | + |
| 39 | + expect(result1).toEqual(result2); |
| 40 | + }); |
| 41 | + |
| 42 | + it('default', () => { |
| 43 | + const result = wordCloud(data); |
| 44 | + const firstRow = result[0]; |
| 45 | + |
| 46 | + expect(firstRow.hasText).toBe(true); |
| 47 | + expect(typeof firstRow.x).toBe('number'); |
| 48 | + expect(typeof firstRow.y).toBe('number'); |
| 49 | + expect(typeof firstRow.text).toBe('string'); |
| 50 | + expect(typeof firstRow.size).toBe('number'); |
| 51 | + expect(typeof firstRow.font).toBe('string'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('callback', () => { |
| 55 | + const common = (row: DataItem) => { |
| 56 | + expect(typeof row.text).toBe('string'); |
| 57 | + expect(typeof row.value).toBe('number'); |
| 58 | + }; |
| 59 | + const font = (row: DataItem) => { |
| 60 | + common(row); |
| 61 | + return 'font-test'; |
| 62 | + }; |
| 63 | + const fontWeight = (row: DataItem): any => { |
| 64 | + common(row); |
| 65 | + return 'fontWeight-test'; |
| 66 | + }; |
| 67 | + const fontSize = (row: DataItem) => { |
| 68 | + common(row); |
| 69 | + return 11; |
| 70 | + }; |
| 71 | + const rotate = (row: DataItem) => { |
| 72 | + common(row); |
| 73 | + return 22; |
| 74 | + }; |
| 75 | + const padding = (row: DataItem) => { |
| 76 | + common(row); |
| 77 | + return 33; |
| 78 | + }; |
| 79 | + const spiral = (size: [number, number]) => { |
| 80 | + expect(size.length).toBe(2); |
| 81 | + const e = size[0] / size[1]; |
| 82 | + return (t: number) => { |
| 83 | + expect(typeof t).toBe('number'); |
| 84 | + return [e * (t *= 0.1) * Math.cos(t), t * Math.sin(t)]; |
| 85 | + }; |
| 86 | + }; |
| 87 | + |
| 88 | + const result = wordCloud(data, { |
| 89 | + font, |
| 90 | + fontWeight, |
| 91 | + fontSize, |
| 92 | + rotate, |
| 93 | + padding, |
| 94 | + spiral, |
| 95 | + }); |
| 96 | + const firstRow = result[0]; |
| 97 | + expect(firstRow.hasText).toBe(true); |
| 98 | + expect(typeof firstRow.x).toBe('number'); |
| 99 | + expect(typeof firstRow.y).toBe('number'); |
| 100 | + expect(typeof firstRow.text).toBe('string'); |
| 101 | + expect(firstRow.font).toBe('font-test'); |
| 102 | + expect(firstRow.weight).toBe('fontWeight-test'); |
| 103 | + expect(firstRow.size).toBe(11); |
| 104 | + expect(firstRow.rotate).toBe(22); |
| 105 | + expect(firstRow.padding).toBe(33); |
| 106 | + }); |
| 107 | +}); |
0 commit comments