-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option `transformFunctions` for the user to provide a list of functions which will have their first argument transformed based on the plugin root and alias
- Loading branch information
Showing
4 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { transform } from 'babel-core'; | ||
import plugin from '../src'; | ||
|
||
|
||
const calls = [ | ||
'customMethod.something', | ||
]; | ||
|
||
describe('custom calls', () => { | ||
const transformerOpts = { | ||
babelrc: false, | ||
plugins: [ | ||
[plugin, { | ||
root: './test/testproject/src', | ||
alias: { | ||
test: './test/testproject/test', | ||
}, | ||
transformFunctions: [ | ||
'customMethod.something', | ||
], | ||
}], | ||
], | ||
}; | ||
|
||
calls.forEach((name) => { | ||
describe(name, () => { | ||
it('should resolve the path based on the root config', () => { | ||
const code = `${name}("components/Header/SubHeader", ...args);`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`${name}("./test/testproject/src/components/Header/SubHeader", ...args);`); | ||
}); | ||
|
||
it('should alias the path', () => { | ||
const code = `${name}("test", ...args);`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`${name}("./test/testproject/test", ...args);`); | ||
}); | ||
|
||
it('should not change a relative path', () => { | ||
const code = `${name}("./utils", ...args);`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`${name}("./utils", ...args);`); | ||
}); | ||
|
||
it('should handle no arguments', () => { | ||
const code = `${name}();`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`${name}();`); | ||
}); | ||
|
||
it('should handle the first argument not being a string literal', () => { | ||
const code = `${name}(path, ...args);`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`${name}(path, ...args);`); | ||
}); | ||
|
||
it('should handle an empty path', () => { | ||
const code = `${name}('', ...args);`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`${name}('', ...args);`); | ||
}); | ||
|
||
it('should ignore the call if the method name is not fully matched (suffix)', () => { | ||
const code = `${name}.after("components/Sidebar/Footer", ...args);`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`${name}.after("components/Sidebar/Footer", ...args);`); | ||
}); | ||
|
||
it('should ignore the call if the method name is not fully matched (prefix)', () => { | ||
const code = `before.${name}("components/Sidebar/Footer", ...args);`; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe(`before.${name}("components/Sidebar/Footer", ...args);`); | ||
}); | ||
}); | ||
}); | ||
}); |