-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (51 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import path from 'path';
import fs from 'fs';
import assert from 'assert';
import { transformFileSync } from 'babel-core';
import transformReactImg from '../src';
import transformSyntaxJSX from 'babel-plugin-syntax-jsx';
import transformReactJSX from 'babel-plugin-transform-react-jsx';
function trim (str) {
return str.replace(/^\s+|\s+$/, '');
}
function runFixture( fixtureName ) {
const fixturesDir = path.join(__dirname, 'fixtures');
const fixtureDir = path.join(fixturesDir, fixtureName);
const actualPath = path.join(fixtureDir, 'actual.js');
const actual = transformFileSync(actualPath, {
plugins: [ transformSyntaxJSX, transformReactJSX, transformReactImg ],
}).code;
const expected = fs.readFileSync(
path.join(fixtureDir, 'expected.js')
).toString();
assert.equal(trim(actual), trim(expected));
}
describe('Transform jsx images', () => {
it('should generate an import for the img src when img src is literal',() => {
runFixture('importImg')
});
it('should generate a unique defaultMember import for each img when multiple img are processed',() => {
runFixture('importMultipleImg')
});
it('should not generate imports when img has a computed src atribute',() => {
runFixture('importNonLiteralsSrc')
});
it('should generate only one import for multiples img when img src are the same',() => {
runFixture('importSameImg')
});
it('should not generate import if img src is a url',() => {
runFixture('ignoreURL')
});
it('should not generate import if img src start with !',() => {
runFixture('overrideTransformation')
});
it('should not generate import if img src is a data URI',() => {
runFixture('ignoreDataUri')
});
it('should import all img in a srcSet',() => {
runFixture('importSrcSet')
});
it('should generate an import for the SVG image href when image href is literal',() => {
runFixture('importSvgImage')
});
});