1import { vol } from 'memfs'; 2 3import { parseXMLAsync } from '../../utils/XML'; 4import { 5 getColorsAsObject, 6 getObjectAsColorsXml, 7 getProjectColorsXMLPathAsync, 8 removeColorItem, 9 setColorItem, 10} from '../Colors'; 11import { buildResourceItem, readResourcesXMLAsync } from '../Resources'; 12 13jest.mock('fs'); 14 15describe(setColorItem, () => { 16 beforeAll(async () => { 17 const directoryJSON = { 18 './android/app/src/main/res/values/colors.xml': '<resources></resources>', 19 }; 20 vol.fromJSON(directoryJSON, '/app'); 21 }); 22 afterAll(async () => { 23 vol.reset(); 24 }); 25 26 it(`gets different themed colors`, async () => { 27 const path = await getProjectColorsXMLPathAsync('/app', { kind: 'values-night' }); 28 expect(path).toBe('/app/android/app/src/main/res/values-night/colors.xml'); 29 }); 30 it(`modifies the colors file`, async () => { 31 const path = await getProjectColorsXMLPathAsync('/app'); 32 expect(path).toBe('/app/android/app/src/main/res/values/colors.xml'); 33 // read the colors object 34 let colors = await readResourcesXMLAsync({ path }); 35 expect(colors).toStrictEqual({ resources: {} }); 36 37 const colorItemToAdd = buildResourceItem({ name: 'somn', value: '#fff000' }); 38 39 // Add a color item 40 colors = setColorItem(colorItemToAdd, colors); 41 // check the object 42 expect(colors).toStrictEqual({ resources: { color: [colorItemToAdd] } }); 43 // change the color 44 colorItemToAdd._ = '#000000'; 45 // reassign the color 46 colors = setColorItem(colorItemToAdd, colors); 47 // check the object is reassigned 48 expect(colors.resources.color[0]._).toBe('#000000'); 49 // ensure an extra color was not added 50 expect(colors.resources.color.length).toBe(1); 51 // Remove the color item 52 colors = removeColorItem('somn', colors); 53 // doesn't fully reset the colors. 54 expect(colors).toStrictEqual({ resources: { color: [] } }); 55 }); 56}); 57 58describe(getColorsAsObject, () => { 59 it(`converts as expected`, async () => { 60 // Parsed from a string for DX readability 61 const xml = await parseXMLAsync( 62 [ 63 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', 64 '<resources>', 65 ' <color name="colorPrimary">#FFBB86FC</color>', 66 ' <color name="navigationBar">red</color>', 67 ' <color name="foobar">@bacon</color>', 68 '</resources>', 69 ].join('\n') 70 ); 71 72 const colors = getColorsAsObject(xml as any); 73 74 expect(colors).toStrictEqual({ 75 colorPrimary: '#FFBB86FC', 76 foobar: '@bacon', 77 navigationBar: 'red', 78 }); 79 }); 80}); 81 82describe(getObjectAsColorsXml, () => { 83 it(`converts object to object matching parsed colors.xml`, async () => { 84 // Parsed from a string for DX readability 85 const colors = [ 86 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', 87 '<resources>', 88 ' <color name="colorPrimary">#FFBB86FC</color>', 89 ' <color name="navigationBar">red</color>', 90 ' <color name="foobar">@bacon</color>', 91 '</resources>', 92 ].join('\n'); 93 94 expect( 95 getObjectAsColorsXml({ colorPrimary: '#FFBB86FC', navigationBar: 'red', foobar: '@bacon' }) 96 ).toStrictEqual(await parseXMLAsync(colors)); 97 }); 98}); 99