Skip to content

Commit e84cf6a

Browse files
committed
Add additional tests for stylex.props
1 parent 6bc28dc commit e84cf6a

File tree

1 file changed

+177
-1
lines changed

1 file changed

+177
-1
lines changed

packages/@stylexjs/babel-plugin/__tests__/transform-stylex-props-test.js

Lines changed: 177 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,35 @@ jest.autoMockOff();
1212
import { transformSync } from '@babel/core';
1313
import jsx from '@babel/plugin-syntax-jsx';
1414
import stylexPlugin from '../src/index';
15+
import path from 'path';
1516

1617
function transform(source, opts = {}) {
1718
return transformSync(source, {
1819
filename: opts.filename,
1920
parserOpts: {
21+
sourceType: 'module',
2022
flow: 'all',
2123
},
22-
plugins: [jsx, [stylexPlugin, { ...opts, runtimeInjection: true }]],
24+
babelrc: false,
25+
plugins: [
26+
jsx,
27+
[
28+
stylexPlugin,
29+
{
30+
unstable_moduleResolution: {
31+
rootDir: __dirname,
32+
type: 'commonJS',
33+
},
34+
...opts,
35+
runtimeInjection: true,
36+
},
37+
],
38+
],
2339
}).code;
2440
}
2541

42+
const THIS_FILE = path.join(__dirname, 'transform-stylex-props-test.js');
43+
2644
describe('@stylexjs/babel-plugin', () => {
2745
describe('[transform] stylex.props() call', () => {
2846
test('empty stylex.props call', () => {
@@ -1794,4 +1812,162 @@ describe('@stylexjs/babel-plugin', () => {
17941812
`);
17951813
});
17961814
});
1815+
1816+
describe('dealing with imports', () => {
1817+
test('all local styles', () => {
1818+
expect(
1819+
transform(
1820+
`
1821+
import * as stylex from '@stylexjs/stylex';
1822+
const styles = stylex.create({
1823+
default: {
1824+
color: 'black',
1825+
},
1826+
red: {
1827+
color: 'red',
1828+
},
1829+
blueBg: {
1830+
backgroundColor: 'blue',
1831+
},
1832+
1833+
});
1834+
1835+
<div {...stylex.props(styles.default, styles.red, styles.blueBg)} />
1836+
`,
1837+
),
1838+
).toMatchInlineSnapshot(`
1839+
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
1840+
var _inject2 = _inject;
1841+
import * as stylex from '@stylexjs/stylex';
1842+
_inject2(".x1mqxbix{color:black}", 3000);
1843+
_inject2(".x1e2nbdu{color:red}", 3000);
1844+
_inject2(".x1t391ir{background-color:blue}", 3000);
1845+
<div className="x1e2nbdu x1t391ir" />;"
1846+
`);
1847+
});
1848+
// TODO: fix this broken test
1849+
test.skip('local array styles', () => {
1850+
expect(
1851+
transform(
1852+
`
1853+
import * as stylex from '@stylexjs/stylex';
1854+
const styles = stylex.create({
1855+
default: {
1856+
color: 'black',
1857+
},
1858+
red: {
1859+
color: 'red',
1860+
},
1861+
blueBg: {
1862+
backgroundColor: 'blue',
1863+
},
1864+
});
1865+
1866+
const base = [styles.default, styles.red];
1867+
1868+
<div {...stylex.props(base, styles.blueBg)} />
1869+
`,
1870+
),
1871+
).toMatchInlineSnapshot();
1872+
});
1873+
test('regular style import', () => {
1874+
expect(
1875+
transform(
1876+
`
1877+
import * as stylex from '@stylexjs/stylex';
1878+
import {someStyle} from './otherFile';
1879+
const styles = stylex.create({
1880+
default: {
1881+
color: 'black',
1882+
},
1883+
});
1884+
<div {...stylex.props(styles.default, someStyle)} />
1885+
`,
1886+
),
1887+
).toMatchInlineSnapshot(`
1888+
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
1889+
var _inject2 = _inject;
1890+
import * as stylex from '@stylexjs/stylex';
1891+
import { someStyle } from './otherFile';
1892+
_inject2(".x1mqxbix{color:black}", 3000);
1893+
const styles = {
1894+
default: {
1895+
kMwMTN: "x1mqxbix",
1896+
$$css: true
1897+
}
1898+
};
1899+
<div {...stylex.props(styles.default, someStyle)} />;"
1900+
`);
1901+
});
1902+
test('default import from .stylex.js file', () => {
1903+
expect(
1904+
transform(
1905+
`
1906+
import * as stylex from '@stylexjs/stylex';
1907+
import {someStyle, vars} from './__fixtures__/constants.stylex.js';
1908+
const styles = stylex.create({
1909+
default: {
1910+
color: 'black',
1911+
backgroundColor: vars.foo,
1912+
},
1913+
});
1914+
<div {...stylex.props(styles.default, someStyle)} />
1915+
`,
1916+
{
1917+
filename: THIS_FILE,
1918+
},
1919+
),
1920+
).toMatchInlineSnapshot(`
1921+
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
1922+
var _inject2 = _inject;
1923+
import * as stylex from '@stylexjs/stylex';
1924+
import { someStyle, vars } from './__fixtures__/constants.stylex.js';
1925+
_inject2(".x1mqxbix{color:black}", 3000);
1926+
_inject2(".x1ptj8da{background-color:var(--xu6xsfm)}", 3000);
1927+
const styles = {
1928+
default: {
1929+
kMwMTN: "x1mqxbix",
1930+
kWkggS: "x1ptj8da",
1931+
$$css: true
1932+
}
1933+
};
1934+
<div {...stylex.props(styles.default, someStyle)} />;"
1935+
`);
1936+
});
1937+
test('object import from .stylex.js file', () => {
1938+
expect(
1939+
transform(
1940+
`
1941+
import * as stylex from '@stylexjs/stylex';
1942+
import {someStyle} from './__fixtures__/constants.stylex.js';
1943+
const styles = stylex.create({
1944+
default: {
1945+
color: 'black',
1946+
backgroundColor: someStyle.foo,
1947+
},
1948+
});
1949+
<div {...stylex.props(styles.default, someStyle.foo)} />
1950+
`,
1951+
{
1952+
filename: THIS_FILE,
1953+
},
1954+
),
1955+
).toMatchInlineSnapshot(`
1956+
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
1957+
var _inject2 = _inject;
1958+
import * as stylex from '@stylexjs/stylex';
1959+
import { someStyle } from './__fixtures__/constants.stylex.js';
1960+
_inject2(".x1mqxbix{color:black}", 3000);
1961+
_inject2(".xxtkuhj{background-color:var(--x18h8e3f)}", 3000);
1962+
const styles = {
1963+
default: {
1964+
kMwMTN: "x1mqxbix",
1965+
kWkggS: "xxtkuhj",
1966+
$$css: true
1967+
}
1968+
};
1969+
<div {...stylex.props(styles.default, someStyle.foo)} />;"
1970+
`);
1971+
});
1972+
});
17971973
});

0 commit comments

Comments
 (0)