@@ -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