|
| 1 | +import { withStringsXml } from '@expo/config-plugins'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { addStringsToXml, withProjectStrings } from '../../plugin/src/android/withProjectStrings'; |
| 5 | +import { getPluginVersion } from '../../plugin/src/utils/plugin'; |
| 6 | + |
| 7 | +jest.mock('@expo/config-plugins'); |
| 8 | + |
| 9 | +const mockWithStringsXml = withStringsXml as jest.MockedFunction<typeof withStringsXml>; |
| 10 | + |
| 11 | +describe('Android Project Strings Configuration', () => { |
| 12 | + beforeEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('Customer.io SDK client metadata strings', () => { |
| 17 | + it('should set client source to "Expo" and version to current plugin version', () => { |
| 18 | + const mockConfig = { |
| 19 | + modResults: { |
| 20 | + resources: { |
| 21 | + string: [] |
| 22 | + } |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + mockWithStringsXml.mockImplementation((config, modifier) => { |
| 27 | + modifier(mockConfig as any); |
| 28 | + return config; |
| 29 | + }); |
| 30 | + |
| 31 | + // Read the actual package.json to get the expected version |
| 32 | + const packageJsonPath = path.resolve(__dirname, '../../package.json'); |
| 33 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 34 | + const expectedVersion = packageJson.version; |
| 35 | + |
| 36 | + withProjectStrings({} as any); |
| 37 | + |
| 38 | + expect(mockConfig.modResults.resources.string).toContainEqual({ |
| 39 | + $: { name: 'customer_io_react_native_sdk_client_source' }, |
| 40 | + _: 'Expo' |
| 41 | + }); |
| 42 | + expect(mockConfig.modResults.resources.string).toContainEqual({ |
| 43 | + $: { name: 'customer_io_react_native_sdk_client_version' }, |
| 44 | + _: expectedVersion |
| 45 | + }); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('addStringsToXml utility', () => { |
| 50 | + it('should add Customer.io SDK strings to empty XML', () => { |
| 51 | + const stringsXml = { |
| 52 | + resources: { |
| 53 | + string: [] |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + const stringResources = [ |
| 58 | + { name: 'customer_io_react_native_sdk_client_source', value: 'Expo' }, |
| 59 | + { name: 'customer_io_react_native_sdk_client_version', value: getPluginVersion() } |
| 60 | + ]; |
| 61 | + |
| 62 | + addStringsToXml(stringsXml, stringResources); |
| 63 | + |
| 64 | + expect(stringsXml.resources.string).toHaveLength(2); |
| 65 | + expect(stringsXml.resources.string[0]).toEqual({ |
| 66 | + $: { name: 'customer_io_react_native_sdk_client_source' }, |
| 67 | + _: 'Expo' |
| 68 | + }); |
| 69 | + expect(stringsXml.resources.string[1]).toEqual({ |
| 70 | + $: { name: 'customer_io_react_native_sdk_client_version' }, |
| 71 | + _: getPluginVersion() |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should update existing Customer.io SDK strings', () => { |
| 76 | + const stringsXml = { |
| 77 | + resources: { |
| 78 | + string: [ |
| 79 | + { |
| 80 | + $: { name: 'customer_io_react_native_sdk_client_source' }, |
| 81 | + _: 'ReactNative' |
| 82 | + }, |
| 83 | + { |
| 84 | + $: { name: 'customer_io_react_native_sdk_client_version' }, |
| 85 | + _: '1.0.0' |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const stringResources = [ |
| 92 | + { name: 'customer_io_react_native_sdk_client_source', value: 'Expo' }, |
| 93 | + { name: 'customer_io_react_native_sdk_client_version', value: getPluginVersion() } |
| 94 | + ]; |
| 95 | + |
| 96 | + addStringsToXml(stringsXml, stringResources); |
| 97 | + |
| 98 | + expect(stringsXml.resources.string).toHaveLength(2); |
| 99 | + expect(stringsXml.resources.string[0]._).toBe('Expo'); |
| 100 | + expect(stringsXml.resources.string[1]._).toBe(getPluginVersion()); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should create XML structure when missing', () => { |
| 104 | + const stringsXml = {}; |
| 105 | + |
| 106 | + const stringResources = [ |
| 107 | + { name: 'customer_io_react_native_sdk_client_source', value: 'Expo' } |
| 108 | + ]; |
| 109 | + |
| 110 | + addStringsToXml(stringsXml as any, stringResources); |
| 111 | + |
| 112 | + expect(stringsXml).toEqual({ |
| 113 | + resources: { |
| 114 | + string: [ |
| 115 | + { |
| 116 | + $: { name: 'customer_io_react_native_sdk_client_source' }, |
| 117 | + _: 'Expo' |
| 118 | + } |
| 119 | + ] |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments