Skip to content

Commit 9700fd7

Browse files
committed
test: add test case
1 parent 4246f1a commit 9700fd7

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

tests/__snapshots__/index.spec.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ exports[`csssinjs > hash 2`] = `":where(.css-dev-only-do-not-override-27r9pa).a,
88

99
exports[`csssinjs > hash 3`] = `":where(.css-dev-only-do-not-override-27r9pa).a,:where(.css-dev-only-do-not-override-27r9pa).b,:where(.css-dev-only-do-not-override-27r9pa).c .d{background:red;}"`;
1010

11+
exports[`csssinjs > hash only changes if salt or cssVar prefix changes 1`] = `":where(.css-dev-only-do-not-override-w8uoem).css-var-test{--rc-test-color-primary:red;}"`;
12+
13+
exports[`csssinjs > hash only changes if salt or cssVar prefix changes 2`] = `":where(.css-dev-only-do-not-override-w8uoem).css-var-test{--rc-test-color-primary:blue;}"`;
14+
1115
exports[`csssinjs > serialize nest object token 1`] = `"hp43io"`;

tests/index.spec.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,4 +828,82 @@ describe('csssinjs', () => {
828828

829829
unmount();
830830
});
831+
832+
it.only('hash only changes if salt or cssVar prefix changes', () => {
833+
const genHashStyle = (token: any): CSSInterpolation => ({
834+
'.box': {
835+
color: token.colorPrimary,
836+
},
837+
});
838+
839+
const Demo = ({
840+
salt,
841+
prefix = 'rc-test',
842+
token: customToken = { colorPrimary: 'red' },
843+
}: {
844+
salt: string;
845+
prefix?: string;
846+
token?: any;
847+
}) => {
848+
const [token, hashId] = useCacheToken<DerivativeToken>(
849+
theme,
850+
[customToken],
851+
{
852+
salt,
853+
cssVar: {
854+
key: 'css-var-test',
855+
prefix,
856+
hashed: true,
857+
},
858+
},
859+
);
860+
861+
useStyleRegister(
862+
{ theme, token, hashId, path: ['test-hash-change'] },
863+
() => [genHashStyle(token)],
864+
);
865+
866+
return <div className={classNames('box', hashId)} />;
867+
};
868+
869+
const { rerender } = render(<Demo salt="test" />);
870+
871+
const styles = Array.from(document.head.querySelectorAll('style'));
872+
expect(styles).toHaveLength(2);
873+
expect(styles[0].innerHTML).toMatchSnapshot();
874+
875+
const style = styles[1].innerHTML;
876+
expect(style).toContain('.box{color:var(--rc-test-color-primary);}');
877+
878+
rerender(<Demo salt="test" />);
879+
const styles2 = Array.from(document.head.querySelectorAll('style'));
880+
expect(styles2).toHaveLength(2);
881+
expect(styles2[1].innerHTML).toContain(
882+
'.box{color:var(--rc-test-color-primary);}',
883+
);
884+
expect(styles2[1].innerHTML).toEqual(style);
885+
886+
// token 不影响样式
887+
rerender(<Demo salt="test" token={{ colorPrimary: 'blue' }} />);
888+
const styles4 = Array.from(document.head.querySelectorAll('style'));
889+
expect(styles4).toHaveLength(2);
890+
expect(styles4[0].innerHTML).toMatchSnapshot();
891+
expect(styles4[1].innerHTML).toContain(
892+
'.box{color:var(--rc-test-color-primary);}',
893+
);
894+
expect(styles4[1].innerHTML).toEqual(style);
895+
896+
rerender(<Demo salt="test2" />);
897+
const styles3 = Array.from(document.head.querySelectorAll('style'));
898+
// 这里生成了新的组件样式,但是 css 变量没变,所以多了一个
899+
expect(styles3).toHaveLength(3);
900+
expect(styles3[1].innerHTML).toContain(
901+
'.box{color:var(--rc-test-color-primary);}',
902+
);
903+
expect(styles3[1].innerHTML).toEqual(style);
904+
expect(styles3[2].innerHTML).toContain(
905+
'.box{color:var(--rc-test-color-primary);}',
906+
);
907+
expect(styles3[2].innerHTML).not.toEqual(style);
908+
});
831909
});

0 commit comments

Comments
 (0)