1- export interface CssAtRuleAST {
2- declarations : StyleDeclaration [ ] ;
3- rules : Rule [ ] ;
4- }
5-
6- interface Rule {
7- declarations : StyleDeclaration [ ] ;
8- selectors : string [ ] ;
9- }
10-
111interface StyleDeclaration extends Record < string , string > {
122 property : string ;
133 value : string ;
144}
155
16- export const normalizeStyles = ( css : Partial < CSSStyleDeclaration > ) :
17- { expectedStyle : StyleDeclaration ; props : string [ ] ; } => {
6+ function normalizeStyles ( css : Partial < CSSStyleDeclaration > ) : StyleDeclaration {
187 const normalizer = document . createElement ( "div" ) ;
198 document . body . appendChild ( normalizer ) ;
209
21- const { props , expectedStyle } = Object . entries ( css ) . reduce (
10+ const { expectedStyle } = Object . entries ( css ) . reduce (
2211 ( acc , [ property , value ] ) => {
2312
2413 if ( typeof value !== "string" ) {
@@ -37,20 +26,50 @@ export const normalizeStyles = (css: Partial<CSSStyleDeclaration>):
3726 ...acc . expectedStyle ,
3827 [ property ] : normalizedValue ,
3928 } ,
40- props : [ ...acc . props , property ] ,
4129 } ;
4230 } ,
43- { expectedStyle : { } as StyleDeclaration , props : [ ] as string [ ] } ,
31+ { expectedStyle : { } as StyleDeclaration } ,
4432 ) ;
4533
4634 document . body . removeChild ( normalizer ) ;
4735
48- return { expectedStyle, props } ;
49- } ;
36+ return expectedStyle ;
37+ }
38+
39+ function getReceivedStyle ( props : string [ ] , received : CSSStyleDeclaration ) : StyleDeclaration {
5040
51- export const getReceivedStyle = ( props : string [ ] , received : CSSStyleDeclaration ) : StyleDeclaration => {
5241 return props . reduce ( ( acc , prop ) => {
53- acc [ prop ] = received ?. getPropertyValue ( prop ) . trim ( ) ;
54- return acc ;
42+
43+ const actualStyle = received . getPropertyValue ( prop ) . trim ( ) ;
44+
45+ return actualStyle
46+ ? { ...acc , [ prop ] : actualStyle }
47+ : acc ;
48+
5549 } , { } as StyleDeclaration ) ;
50+ }
51+
52+ export const getExpectedAndReceivedStyles =
53+ ( actual : Element , expected : Partial < CSSStyleDeclaration > ) : StyleDeclaration [ ] => {
54+ if ( ! actual . ownerDocument . defaultView ) {
55+ throw new Error ( "The element is not attached to a document with a default view." ) ;
56+ }
57+ if ( ! ( actual instanceof HTMLElement ) ) {
58+ throw new Error ( "The element is not an HTMLElement." ) ;
59+ }
60+
61+ const window = actual . ownerDocument . defaultView ;
62+
63+ const rawElementStyles = window . getComputedStyle ( actual ) ;
64+
65+ const expectedStyle = normalizeStyles ( expected ) ;
66+
67+ const styleKeys = Object . keys ( expectedStyle ) ;
68+
69+ const elementProcessedStyle = getReceivedStyle ( styleKeys , rawElementStyles ) ;
70+
71+ return [
72+ expectedStyle ,
73+ elementProcessedStyle ,
74+ ] ;
5675} ;
0 commit comments