import { astish } from '../astish'; describe('astish', () => { it('regular', () => { expect( astish(` prop: value; `) ).toEqual({ prop: 'value' }); }); it('nested', () => { expect( astish(` prop: value; @keyframes foo { 0% { attr: value; } 50% { opacity: 1; } 100% { foo: baz; } } named { background-image: url('/path-to-jpg.png'); } opacity: 0; .class, &:hover { -webkit-touch: none; } `) ).toEqual({ prop: 'value', opacity: '0', '.class, &:hover': { '-webkit-touch': 'none' }, '@keyframes foo': { '0%': { attr: 'value' }, '50%': { opacity: '1' }, '100%': { foo: 'baz' } }, named: { 'background-image': "url('/path-to-jpg.png')" } }); }); it('merging', () => { expect( astish(` .c { font-size:24px; } .c { color:red; } `) ).toEqual({ '.c': { 'font-size': '24px', color: 'red' } }); }); it('regression', () => { expect( astish(` &.g0ssss { aa: foo; box-shadow: 0 1px rgba(0, 2, 33, 4) inset; } named { transform: scale(1.2), rotate(1, 1); } @media screen and (some-rule: 100px) { foo: baz; opacity: 1; level { one: 1; level { two: 2; } } } .a{ color: red; } .b { color: blue; } `) ).toEqual({ '&.g0ssss': { aa: 'foo', 'box-shadow': '0 1px rgba(0, 2, 33, 4) inset' }, '.a': { color: 'red' }, '.b': { color: 'blue' }, named: { transform: 'scale(1.2), rotate(1, 1)' }, '@media screen and (some-rule: 100px)': { foo: 'baz', opacity: '1', level: { one: '1', level: { two: '2' } } } }); }); it('should strip comments', () => { expect( astish(` color: red; /* some comment */ transform: translate3d(0, 0, 0); /** * other comment */ background: peachpuff; font-size: xx-large; /* inline comment */ /* foo: bar */ font-weight: bold; `) ).toEqual({ color: 'red', transform: 'translate3d(0, 0, 0)', background: 'peachpuff', 'font-size': 'xx-large', 'font-weight': 'bold' }); }); // for reference on what is valid: // https://www.w3.org/TR/CSS22/syndata.html#value-def-identifier it('should not mangle valid css identifiers', () => { expect( astish(` :root { --azAZ-_中文09: 0; } `) ).toEqual({ ':root': { '--azAZ-_中文09': '0' } }); }); it('should parse multiline background declaration', () => { expect( astish(` background: url('data:image/svg+xml,') center/contain; `) ).toEqual({ background: `url('data:image/svg+xml,') center/contain` }); }); it('should handle inline @media block', () => { expect( astish( `h1 { font-size: 1rem; } @media only screen and (min-width: 850px) { h1 { font-size: 2rem; } }` ) ).toEqual({ h1: { 'font-size': '1rem' }, '@media only screen and (min-width: 850px)': { h1: { 'font-size': '2rem' } } }); }); it('should handle newlines as part of the rule value', () => { expect( astish( `tag { font-size: first second; }` ) ).toEqual({ tag: { 'font-size': 'first second' } }); }); });