|
| 1 | +import { |
| 2 | + addValueToProperty, |
| 3 | + appendToKeys, |
| 4 | + calculateObsoleteFrames, |
| 5 | + calculateObsoleteValues, |
| 6 | + getInterpolator, |
| 7 | + omitEmptyValues, |
| 8 | + toString, |
| 9 | +} from '../util' |
| 10 | + |
| 11 | +describe('util', () => { |
| 12 | + describe('getInterpolator', () => { |
| 13 | + test('returns interpolator method that takes 3 params', () => { |
| 14 | + const interpolate = getInterpolator(180, 12) |
| 15 | + expect(interpolate.length).toEqual(3) |
| 16 | + }) |
| 17 | + test('interpolator method returns an array of 101 values', () => { |
| 18 | + const interpolate = getInterpolator(180, 12) |
| 19 | + const values = interpolate(0, 100) |
| 20 | + expect(values).toEqual(expect.any(Array)) |
| 21 | + expect(values.length).toEqual(101) |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + describe('addValueToProperty', () => { |
| 26 | + test('defaults to empty object', () => { |
| 27 | + expect(addValueToProperty({}.foo, 'bar', 'baz')) |
| 28 | + .toEqual({ bar: 'baz' }) |
| 29 | + }) |
| 30 | + test('adds property besides other properties', () => { |
| 31 | + expect(addValueToProperty({ foo: 'bar' }, 'baz', 'qux')) |
| 32 | + .toEqual({ foo: 'bar', baz: 'qux' }) |
| 33 | + }) |
| 34 | + test('adds value to array with existing values', () => { |
| 35 | + expect(addValueToProperty({ foo: 'bar' }, 'foo', 'baz')) |
| 36 | + .toEqual({ foo: [ 'bar', 'baz' ]}) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('calculateObsoleteValues', () => { |
| 41 | + test('calculates the obsolete values', () => { |
| 42 | + const data = { |
| 43 | + 0: { 'margin-left': '0px', border: [ '1px', 'solid', '#f00' ]}, |
| 44 | + 1: { 'margin-left': '1px', border: [ '2px', 'solid', '#f00' ]}, |
| 45 | + 2: { 'margin-left': '1px', border: [ '2px', 'solid', '#f00' ]}, |
| 46 | + 3: { 'margin-left': '2px', border: [ '2px', 'solid', '#f00' ]}, |
| 47 | + 4: { 'margin-left': '2px', border: [ '3px', 'solid', '#f00' ]}, |
| 48 | + 5: { 'margin-left': '2px', border: [ '3px', 'solid', '#f00' ]}, |
| 49 | + 6: { 'margin-left': '3px', border: [ '3px', 'solid', '#f00' ]}, |
| 50 | + 7: { 'margin-left': '3px', border: [ '3px', 'solid', '#f00' ]}, |
| 51 | + } |
| 52 | + |
| 53 | + expect(calculateObsoleteValues(data)).toEqual({ |
| 54 | + border: [ 2, 5, 6 ], |
| 55 | + 'margin-left': [ 4 ], |
| 56 | + }) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('calculateObsoleteFrames', () => { |
| 61 | + test('calculates the obsolete frames', () => { |
| 62 | + const data = [ |
| 63 | + { 'margin-left': '0px', border: [ '1px', 'solid', '#f00' ]}, |
| 64 | + { 'margin-left': '1px', border: [ '2px', 'solid', '#f00' ]}, |
| 65 | + { 'margin-left': '1px', border: [ '2px', 'solid', '#f00' ]}, |
| 66 | + { 'margin-left': '2px', border: [ '2px', 'solid', '#f00' ]}, |
| 67 | + { 'margin-left': '2px', border: [ '3px', 'solid', '#f00' ]}, |
| 68 | + { 'margin-left': '2px', border: [ '3px', 'solid', '#f00' ]}, |
| 69 | + { 'margin-left': '3px', border: [ '3px', 'solid', '#f00' ]}, |
| 70 | + { 'margin-left': '3px', border: [ '3px', 'solid', '#f00' ]}, |
| 71 | + ] |
| 72 | + |
| 73 | + expect(calculateObsoleteFrames(data, 'border')).toEqual([ 2, 5, 6 ]) |
| 74 | + expect(calculateObsoleteFrames(data, 'margin-left')).toEqual([ 4 ]) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('appendToKeys', () => { |
| 79 | + test('appends string to every key', () => { |
| 80 | + const appended = appendToKeys({ 0: 0, 1: 1, 2: 2 }, '%') |
| 81 | + |
| 82 | + expect(Object.keys(appended)) |
| 83 | + .toEqual([ '0%', '1%', '2%' ]) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + describe('omitEmptyValues', () => { |
| 88 | + test('omits empty values', () => { |
| 89 | + const omitted = omitEmptyValues({ |
| 90 | + '0%': { opacity: 0, left: '20px' }, |
| 91 | + '1%': {}, |
| 92 | + '100%': { opacity: 1, left: '200px' }, |
| 93 | + }) |
| 94 | + |
| 95 | + expect(Object.keys(omitted)) |
| 96 | + .toEqual([ '0%', '100%' ]) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe('toString', () => { |
| 101 | + test('returns formatted css string', () => { |
| 102 | + const formatted = toString({ |
| 103 | + '0%': { opacity: 0, left: '20px' }, |
| 104 | + '100%': { opacity: 1, left: '200px' }, |
| 105 | + }) |
| 106 | + |
| 107 | + expect(formatted) |
| 108 | + .toEqual('0%{opacity:0;left:20px;}100%{opacity:1;left:200px;}') |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments