Skip to content

Commit

Permalink
test: use same token object in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonambas committed Apr 6, 2024
1 parent dbd7764 commit 012200a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { codegen } from '../codegen';
import { createContext } from '../context';

const context = createContext({
foo: { 100: { value: { base: 'whitesmoke', lg: 'palegreen' } } },
bar: { 100: '{colors.green.200}' },
foo: { 100: { value: '#fff' }, 200: { value: { base: '#000' } } },
bar: { 100: 'red', 200: 'blue' },
});

const args: CodegenPrepareHookArgs = {
Expand All @@ -26,7 +26,7 @@ describe('codegen', () => {
expect(result[0].files[0]).toMatchInlineSnapshot(`
{
"code": "
const pluginCtMap = new Map(JSON.parse('[["foo.100",{"base":"whitesmoke","lg":"palegreen"}],["bar.100","{colors.green.200}"]]'));
const pluginCtMap = new Map(JSON.parse('[["foo.100","#fff"],["foo.200",{"base":"#000"}],["bar.100","red"],["bar.200","blue"]]'));
export const ct = (path) => {
if (!path) return 'panda-plugin-ct-path-empty';
Expand All @@ -41,7 +41,7 @@ describe('codegen', () => {
expect(result[0].files[1]).toMatchInlineSnapshot(`
{
"code": "
export const ct: (alias: "foo.100" | "bar.100") => string;",
export const ct: (alias: "foo.100" | "foo.200" | "bar.100" | "bar.200") => string;",
"file": "css.d.ts",
}
`);
Expand Down
14 changes: 6 additions & 8 deletions src/__tests__/ct.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { ct, ctTemplate } from '../ct';

const tokens = {
foo: { a: { b: { c: { value: { base: '10px', lg: '20px' } } } } },
bar: { baz: { 100: { value: {} }, 200: { value: {} } } },
baz: { 100: 'hello', 200: { value: 'goodbye' } },
foo: { 100: { value: '#fff' }, 200: { value: { base: '#000' } } },
bar: { 100: 'red', 200: 'blue' },
};

describe('ct', () => {
it('gets a string', () => {
expect(ct(tokens, 'baz.100')).toBe('hello');
expect(ct(tokens, 'bar.100')).toBe('red');
});

it('gets a value object', () => {
expect(ct(tokens, 'foo.a.b.c')).toMatchInlineSnapshot(
expect(ct(tokens, 'foo.200')).toMatchInlineSnapshot(
`
{
"base": "10px",
"lg": "20px",
"base": "#000",
}
`,
);
});

it('gets a value string', () => {
expect(ct(tokens, 'baz.200')).toMatchInlineSnapshot(`"goodbye"`);
expect(ct(tokens, 'foo.100')).toMatchInlineSnapshot(`"#fff"`);
});

it('gets an undefined token', () => {
Expand Down
21 changes: 4 additions & 17 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { parser } from '../parser';
import { createContext } from '../context';

const context = createContext({
foo: { 100: { value: { base: 'whitesmoke', lg: 'palegreen' } } },
bar: { 100: '{colors.green.200}' },
foo: { 100: { value: '#fff' }, 200: { value: { base: '#000' } } },
bar: { 100: 'red', 200: 'blue' },
});

describe('parser', () => {
Expand All @@ -12,13 +12,13 @@ describe('parser', () => {
{
configure: () => {},
filePath: 'test.tsx',
content: `<div className={css({ bg: ct("foo.100"), color: ct('bar.100'))})/>`,
content: `<div className={css({ bg: ct("foo.200"), color: ct('bar.100'))})/>`,
},
context,
);

expect(res).toMatchInlineSnapshot(
`"<div className={css({ bg: {"base":"whitesmoke","lg":"palegreen"}, color: '{colors.green.200}')})/>"`,
`"<div className={css({ bg: {"base":"#000"}, color: 'red')})/>"`,
);
});

Expand All @@ -34,17 +34,4 @@ describe('parser', () => {

expect(res).toBeUndefined();
});

it('skips without a path', () => {
const res = parser(
{
configure: () => {},
filePath: 'test.tsx',
content: `<div className={css({ bg: ct("") })/>`,
},
context,
);

expect(res).toMatchInlineSnapshot(`"<div className={css({ bg: ct("") })/>"`);
});
});

0 comments on commit 012200a

Please sign in to comment.