|
4 | 4 | * @typedef {import('../lib/types').Plugin} Plugin |
5 | 5 | */ |
6 | 6 |
|
7 | | -const os = require('os'); |
8 | | -const path = require('path'); |
9 | 7 | const { optimize, loadConfig } = require('./svgo-node.js'); |
10 | 8 |
|
11 | | -const describeLF = os.EOL === '\r\n' ? describe.skip : describe; |
12 | | -const describeCRLF = os.EOL === '\r\n' ? describe : describe.skip; |
13 | | - |
14 | | -describeLF('with LF line-endings', () => { |
| 9 | +describe('with LF line-endings', () => { |
15 | 10 | test('should work', () => { |
16 | 11 | const svg = ` |
17 | 12 | <?xml version="1.0" encoding="utf-8"?> |
@@ -68,144 +63,14 @@ describeLF('with LF line-endings', () => { |
68 | 63 | }); |
69 | 64 | }); |
70 | 65 |
|
71 | | -describeCRLF('with CRLF line-endings', () => { |
72 | | - test('should work', () => { |
73 | | - const svg = ` |
74 | | - <?xml version="1.0" encoding="utf-8"?> |
75 | | - <svg viewBox="0 0 120 120"> |
76 | | - <desc> |
77 | | - Not standard description |
78 | | - </desc> |
79 | | - <circle fill="#ff0000" cx="60" cy="60" r="50"/> |
80 | | - </svg> |
81 | | - `; |
82 | | - const { data } = optimize(svg); |
83 | | - // using toEqual because line endings matter in these tests |
84 | | - expect(data).toEqual( |
85 | | - '<svg viewBox="0 0 120 120"><circle cx="60" cy="60" r="50" fill="red"/></svg>' |
86 | | - ); |
87 | | - }); |
88 | | - |
89 | | - test('should respect config', () => { |
90 | | - const svg = ` |
91 | | - <?xml version="1.0" encoding="utf-8"?> |
92 | | - <svg viewBox="0 0 120 120"> |
93 | | - <desc> |
94 | | - Not standard description |
95 | | - </desc> |
96 | | - <circle fill="#ff0000" cx="60" cy="60" r="50"/> |
97 | | - </svg> |
98 | | - `; |
99 | | - const { data } = optimize(svg, { |
100 | | - js2svg: { pretty: true, indent: 2 }, |
101 | | - }); |
102 | | - // using toEqual because line endings matter in these tests |
103 | | - expect(data).toEqual( |
104 | | - '<svg viewBox="0 0 120 120">\r\n <circle cx="60" cy="60" r="50" fill="red"/>\r\n</svg>\r\n' |
105 | | - ); |
106 | | - }); |
107 | | - |
108 | | - test('should respect line-ending config', () => { |
109 | | - const svg = ` |
110 | | - <?xml version="1.0" encoding="utf-8"?> |
111 | | - <svg viewBox="0 0 120 120"> |
112 | | - <desc> |
113 | | - Not standard description |
114 | | - </desc> |
115 | | - <circle fill="#ff0000" cx="60" cy="60" r="50"/> |
116 | | - </svg> |
117 | | - `; |
118 | | - const { data } = optimize(svg, { |
119 | | - js2svg: { eol: 'lf', pretty: true, indent: 2 }, |
120 | | - }); |
121 | | - // using toEqual because line endings matter in these tests |
122 | | - expect(data).toEqual( |
123 | | - '<svg viewBox="0 0 120 120">\n <circle fill="red" cx="60" cy="60" r="50"/>\n</svg>\n' |
124 | | - ); |
125 | | - }); |
126 | | -}); |
127 | | - |
128 | 66 | describe('loadConfig', () => { |
129 | | - const cwd = process.cwd(); |
130 | | - const fixtures = path.join(cwd, './test/fixtures/config-loader'); |
131 | | - |
132 | | - test('loads by absolute path', async () => { |
133 | | - expect(await loadConfig(path.join(fixtures, 'one/two/config.js'))).toEqual({ |
134 | | - plugins: [], |
135 | | - }); |
136 | | - }); |
137 | | - |
138 | | - test('loads by relative path to cwd', async () => { |
139 | | - const config = await loadConfig('one/two/config.js', fixtures); |
140 | | - expect(config).toEqual({ plugins: [] }); |
141 | | - }); |
142 | | - |
143 | | - test('searches in cwd and up', async () => { |
144 | | - expect(await loadConfig(null, path.join(fixtures, 'one/two'))).toEqual({ |
145 | | - plugins: [], |
146 | | - }); |
147 | | - expect( |
148 | | - await loadConfig(null, path.join(cwd, './test/fixtures/missing')) |
149 | | - ).toEqual(null); |
150 | | - expect(await loadConfig(null, path.join(fixtures, 'mjs'))).toEqual({ |
151 | | - plugins: ['mjs'], |
152 | | - }); |
153 | | - expect(await loadConfig(null, path.join(fixtures, 'cjs'))).toEqual({ |
154 | | - plugins: ['cjs'], |
155 | | - }); |
156 | | - }); |
157 | | - |
158 | | - test('fails when specified config does not exist', async () => { |
159 | | - try { |
160 | | - await loadConfig('{}'); |
161 | | - expect.fail('Config is loaded successfully'); |
162 | | - } catch (error) { |
163 | | - expect(error.message).toMatch(/Cannot find module/); |
164 | | - } |
165 | | - }); |
166 | | - |
167 | | - test('fails when exported config not an object', async () => { |
168 | | - try { |
169 | | - await loadConfig(path.join(fixtures, 'invalid-null.js')); |
170 | | - expect.fail('Config is loaded successfully'); |
171 | | - } catch (error) { |
172 | | - expect(error.message).toMatch(/Invalid config file/); |
173 | | - } |
174 | | - try { |
175 | | - await loadConfig(path.join(fixtures, 'invalid-array.js')); |
176 | | - expect.fail('Config is loaded successfully'); |
177 | | - } catch (error) { |
178 | | - expect(error.message).toMatch(/Invalid config file/); |
179 | | - } |
180 | | - try { |
181 | | - await loadConfig(path.join(fixtures, 'invalid-string.js')); |
182 | | - expect.fail('Config is loaded successfully'); |
183 | | - } catch (error) { |
184 | | - expect(error.message).toMatch(/Invalid config file/); |
185 | | - } |
186 | | - }); |
187 | | - |
188 | | - test('handles runtime errors properly', async () => { |
189 | | - try { |
190 | | - await loadConfig(path.join(fixtures, 'invalid-runtime.js')); |
191 | | - expect.fail('Config is loaded successfully'); |
192 | | - } catch (error) { |
193 | | - expect(error.message).toMatch(/plugins is not defined/); |
194 | | - } |
195 | | - try { |
196 | | - await loadConfig(path.join(fixtures, 'invalid-runtime.mjs')); |
197 | | - expect.fail('Config is loaded successfully'); |
198 | | - } catch (error) { |
199 | | - expect(error.message).toMatch(/plugins is not defined/); |
200 | | - } |
201 | | - }); |
202 | | - |
203 | | - test('handles MODULE_NOT_FOUND properly', async () => { |
204 | | - try { |
205 | | - await loadConfig(path.join(fixtures, 'module-not-found.js')); |
206 | | - expect.fail('Config is loaded successfully'); |
207 | | - } catch (error) { |
208 | | - expect(error.message).toMatch(/Cannot find module 'unknown-module'/); |
209 | | - } |
| 67 | + test('fails with not implemented error', async () => { |
| 68 | + await expect( |
| 69 | + loadConfig('something', 'something-else') |
| 70 | + ).rejects.toThrowError( |
| 71 | + 'Not implemented error. React Native does not support dynamic imports. ' + |
| 72 | + 'This package (`react-native-svgo`) is meant to be used with react-native, ' + |
| 73 | + 'if you are not using react-native please consider using the original `https://github.com/svg/svgo` package.' |
| 74 | + ); |
210 | 75 | }); |
211 | 76 | }); |
0 commit comments