test-style-mod.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {StyleModule} from "style-mod"
  2. import ist from "ist"
  3. describe("StyleModule", () => {
  4. it("renders objects to CSS text", () => {
  5. ist(rules(new StyleModule({main: {color: "red", border: "1px solid green"}})),
  6. ["main {color: red; border: 1px solid green;}"], eqRules)
  7. })
  8. it("handles multiple rules", () => {
  9. ist(rules(new StyleModule({
  10. one: {color: "green"},
  11. two: {color: "blue"}
  12. })), [
  13. "one {color: green;}",
  14. "two {color: blue;}"
  15. ], eqRules)
  16. })
  17. it("supports &-nesting", () => {
  18. ist(rules(new StyleModule({
  19. main: {
  20. color: "yellow",
  21. "&:hover": {fontWeight: "bold"}
  22. }
  23. })), [
  24. "main:hover {font-weight: bold;}",
  25. "main {color: yellow;}"
  26. ], eqRules)
  27. })
  28. it("can replace multiple & markers", () => {
  29. ist(rules(new StyleModule({
  30. main: {
  31. "p &, div &": {color: "blue"}
  32. }
  33. })), [
  34. "p main, div main {color: blue;}"
  35. ], eqRules)
  36. })
  37. it("supports media queries", () => {
  38. ist(rules(new StyleModule({
  39. "@media screen and (min-width: 400px)": {
  40. main: {
  41. fontFamily: '"URW Bookman"',
  42. MozBoxSizing: "border-box"
  43. }
  44. }
  45. })), ["@media screen and (min-width: 400px) {main {font-family: \"URW Bookman\"; -moz-box-sizing: border-box;}}"], eqRules)
  46. })
  47. it("can render keyframes", () => {
  48. ist(rules(new StyleModule({
  49. "@keyframes foo": {
  50. "0%": {color: "blue"},
  51. "50%": {color: "red"}
  52. }
  53. })), ["@keyframes foo {0% {color: blue;} 50% {color: red;}}"], eqRules)
  54. })
  55. it("doesn't mangle keyframe names", () => {
  56. ist(rules(new StyleModule({
  57. "@keyframes foo": {
  58. "0%": {color: "blue"},
  59. "50%": {color: "red"}
  60. }
  61. }, {finish: s => ".foo " + s})), ["@keyframes foo {0% {color: blue;} 50% {color: red;}}"], eqRules)
  62. })
  63. it("can render multiple instances of a property", () => {
  64. ist(rules(new StyleModule({
  65. main: {
  66. color: "rgba(100, 100, 100, .5)",
  67. color_2: "grey"
  68. }
  69. })), ["main {color: rgba(100, 100, 100, .5); color: grey;}"], eqRules)
  70. })
  71. it("can expand multiple selectors at once", () => {
  72. ist(rules(new StyleModule({
  73. "one, two": {
  74. "&.x": {
  75. color: "yellow"
  76. }
  77. }
  78. })), ["one.x, two.x {color: yellow;}"], eqRules)
  79. })
  80. it("allows processing of selectors", () => {
  81. ist(rules(new StyleModule({
  82. "abc, cba": {color: "yellow"},
  83. "@media stuff": {abc: {fontWeight: "bold"}}
  84. }, {
  85. finish: x => x.replace(/a/g, "u")
  86. })), ["ubc, cbu {color: yellow;}", "@media stuff {ubc {font-weight: bold;}}"], eqRules)
  87. })
  88. })
  89. function rules(module) { return module.rules }
  90. function eqRules(a, b) {
  91. return JSON.stringify(a) == JSON.stringify(b)
  92. }