|
| 1 | +import { |
| 2 | + isAccessibleStylesheet, |
| 3 | + getCSSCustomProps, |
| 4 | + getCSSCustomPropsForElement, |
| 5 | + propSet, |
| 6 | + propGet, |
| 7 | + propsToObject |
| 8 | +} from '../../../../../src/apps/weblib/js-api/utils.js'; |
| 9 | + |
| 10 | + |
| 11 | +describe('utils.isAccessibleStylesheet()', () => { |
| 12 | + test('true -> can access `.cssRules`', () => { |
| 13 | + let stylesheet = { cssRules: [] }; |
| 14 | + expect(isAccessibleStylesheet(stylesheet)).toBeTruthy(); |
| 15 | + }); |
| 16 | + |
| 17 | + test('false -> cannot access `.cssRules`', () => { |
| 18 | + let stylesheet = { get cssRules () { throw new Error('not accessible'); } }; |
| 19 | + expect(isAccessibleStylesheet(stylesheet)).toBeFalsy(); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe('utils.getCSSCustomProps()', () => { |
| 24 | + test('no stylesheet, no props', () => { |
| 25 | + global.document = {styleSheets: []}; // mockig document |
| 26 | + expect(getCSSCustomProps()).toEqual([]); |
| 27 | + }); |
| 28 | + test('empty stylesheets, no props', () => { |
| 29 | + global.document = {styleSheets: [ |
| 30 | + {cssRules: []}, |
| 31 | + {cssRules: []} |
| 32 | + ]}; |
| 33 | + expect(getCSSCustomProps()).toEqual([]); |
| 34 | + }); |
| 35 | + test('stylesheet with proper rules, props expected', () => { |
| 36 | + global.document = {styleSheets: [ |
| 37 | + {cssRules: [ |
| 38 | + {type: 1, style: ['--test-property', '--test-property-2']} |
| 39 | + ]} |
| 40 | + ]}; |
| 41 | + expect(getCSSCustomProps()).toEqual(['--test-property', '--test-property-2']); |
| 42 | + }); |
| 43 | + test('stylesheet with proper rules, using prefix, props expected', () => { |
| 44 | + global.document = {styleSheets: [ |
| 45 | + {cssRules: [ |
| 46 | + {type: 1, style: ['--test-property', '--no-test-property']} |
| 47 | + ]} |
| 48 | + ]}; |
| 49 | + expect(getCSSCustomProps('test')).toEqual(['--test-property']); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('utils.getCSSCustomPropsForElement()', () => { |
| 54 | + test('only element related props should show up', () => { |
| 55 | + global.document = {styleSheets: [ |
| 56 | + {cssRules: [ |
| 57 | + {type: 1, style: ['--test-property', '--test-property-2']} |
| 58 | + ]} |
| 59 | + ]}; |
| 60 | + global.getComputedStyle = () => { |
| 61 | + return { |
| 62 | + getPropertyValue: (prop) => { |
| 63 | + if (prop === '--test-property') { |
| 64 | + return 'test'; |
| 65 | + } |
| 66 | + return ''; |
| 67 | + } |
| 68 | + }; |
| 69 | + }; |
| 70 | + expect(getCSSCustomPropsForElement("whatever")).toEqual([['--test-property', 'test']]); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('utils.propSet()', () => { |
| 75 | + test('set embedded property on empty object', () => { |
| 76 | + const obj = {}; |
| 77 | + propSet(obj, ['alma', 'beka', 'cica'], 'test'); |
| 78 | + expect(obj?.alma?.beka?.cica).toEqual('test'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('does not overwrite by default', () => { |
| 82 | + const obj = {alma: {beka: {cica: 'notest'}}}; |
| 83 | + propSet(obj, ['alma', 'beka', 'cica'], 'test'); |
| 84 | + expect(obj?.alma?.beka?.cica).toEqual('notest'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('can overwrite if requested', () => { |
| 88 | + const obj = {alma: {beka: {cica: 'notest'}}}; |
| 89 | + propSet(obj, ['alma', 'beka', 'cica'], 'test', true); |
| 90 | + expect(obj?.alma?.beka?.cica).toEqual('test'); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('utils.propGet()', () => { |
| 95 | + test('get embedded property', () => { |
| 96 | + const obj = {alma: {beka: {cica: 'test'}}}; |
| 97 | + expect(propGet(obj, ['alma', 'beka', 'cica'])).toEqual('test'); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('utils.propsToObject()', () => { |
| 102 | + test('generate "deep" object from property list', () => { |
| 103 | + const props = [ |
| 104 | + ["--alma-beka-cica", "test"] |
| 105 | + ]; |
| 106 | + const obj = propsToObject(props, null); |
| 107 | + expect(propGet(obj, ['alma', 'beka', 'cica'])).toEqual('test'); |
| 108 | + }); |
| 109 | + |
| 110 | + test('generate "deep" object from property list, using prefix', () => { |
| 111 | + const props = [ |
| 112 | + ["--test-alma-beka-cica", "test"] |
| 113 | + ]; |
| 114 | + const obj = propsToObject(props, null, "test"); |
| 115 | + expect(propGet(obj, ['alma', 'beka', 'cica'])).toEqual('test'); |
| 116 | + }); |
| 117 | + |
| 118 | + test('fill existing "deep" object from property list, using prefix, no overwrite', () => { |
| 119 | + const props = [ |
| 120 | + ["--test-alma-beka-cica", "test"] |
| 121 | + ]; |
| 122 | + const obj = {alma: {beka: {cica: 'notest'}}}; |
| 123 | + propsToObject(props, obj, "test"); |
| 124 | + expect(propGet(obj, ['alma', 'beka', 'cica'])).toEqual('notest'); |
| 125 | + }); |
| 126 | + |
| 127 | + test('fill existing "deep" object from property list, using prefix, overwrite', () => { |
| 128 | + const props = [ |
| 129 | + ["--test-alma-beka-cica", "test"] |
| 130 | + ]; |
| 131 | + const obj = {alma: {beka: {cica: 'notest'}}}; |
| 132 | + propsToObject(props, obj, "test", true); |
| 133 | + expect(propGet(obj, ['alma', 'beka', 'cica'])).toEqual('test'); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
0 commit comments