@hero-design/snowflake-guard
Version:
A hero-design bot detecting snowflake usage
124 lines (121 loc) • 4.09 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const reportCustomStyleProperties_1 = __importDefault(require("../reportCustomStyleProperties"));
const testUtils_1 = require("../testUtils");
const componentList = {
Card: 'Card',
Button: 'Button',
};
const commentList = {
styleCmts: [],
};
describe('reportCustomStyleProperties', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should detect component with inline styles', () => {
const source = `const styles = {
card: {
padding: 10,
},
};
const App = () => (
<Card style={styles.card}><Content/></Card>
);
`;
const ast = (0, testUtils_1.parseTypeScript)(source);
const result = (0, reportCustomStyleProperties_1.default)(ast, componentList, commentList);
expect(result).toEqual({
style: [8],
violatingAttributes: [
{
attributeName: 'padding',
attributeValue: '10',
componentName: 'Card',
inlineStyleProps: 'style',
loc: 8,
},
],
});
});
it('should detect compound component with inline styles', () => {
const source = `const App = () => (
<Button.Icon style={{ padding: 10 }} />
);
`;
const ast = (0, testUtils_1.parseTypeScript)(source);
const result = (0, reportCustomStyleProperties_1.default)(ast, componentList, commentList);
expect(result).toEqual({
style: [2],
violatingAttributes: [
{
attributeName: 'padding',
attributeValue: '10',
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 2,
},
],
});
});
it('should detect compound component using spread operator with inline styles', () => {
const source = `const { Icon } = Button;
const App = () => (
<Icon style={{ padding: 10 }} />
);
`;
const ast = (0, testUtils_1.parseTypeScript)(source);
const result = (0, reportCustomStyleProperties_1.default)(ast, componentList, commentList);
expect(result).toEqual({
style: [3],
violatingAttributes: [
{
attributeName: 'padding',
attributeValue: '10',
componentName: 'Button.Icon',
inlineStyleProps: 'style',
loc: 3,
},
],
});
});
it('should not detect non-custom style properties', () => {
const source = `
const App = () => (
<Card><Content/></Card>
);
`;
const ast = (0, testUtils_1.parseTypeScript)(source);
const result = (0, reportCustomStyleProperties_1.default)(ast, componentList, commentList);
expect(result).toEqual({ style: [], violatingAttributes: [] });
});
it('should ignore approved inline styles', () => {
const mockedCommentList = {
styleCmts: [
{
loc: 10,
comment: '@snowflake-guard/approved-inline-style attributes: padding',
},
],
};
const source = `
const styles = {
button: {
padding: 10,
},
};
const App = () => (
<>
{/* @snowflake-guard/approved-inline-style attributes: padding */}
<Button style={styles.button} />
</>
);
`;
const ast = (0, testUtils_1.parseTypeScript)(source);
const result = (0, reportCustomStyleProperties_1.default)(ast, componentList, mockedCommentList);
expect(result).toEqual({ style: [], violatingAttributes: [] });
});
});