We (my team) is building design system which consists of number web-components (e.g button, link etc) using lit-elements. Design system comes with spec definition which is documented on our documentation site. We want test CSS class properties applied on the web-components.
We can partially test those using window.getComputedStyle()
as follows :-
it('test CSS class properties',()=>{
const element = document.querySelector('selector');
const styles = window.getComputedStyle(element );
const content_property = style.getPropertyValue('justify-content');
expect(content_property).toEqual('space-between');
});
Here comes the problem, when it renders in the browser, computed style property is different than it is defined in the style-sheet.
e.g width:100%
which becomes width:1366px
(the length of the container).
Right now we are testing only one web-component at time in headless browser which means no CSS overrides.
Is there anyway to compute styles the same way the browser does using JavaScript method/API? So we can have computed CSS class property ahead of time and we can compare it with browser's computed style property.
The ugly way of doing it is creating a dummy element with same class from spec documentation in the browser and check it's CSS class properties against original web-component's CSS class properties.
Any suggestions appreciated!