You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

css.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* globals describe, expect, it */
  2. import { Rect } from '../../../../src/main.js'
  3. describe('css.js', () => {
  4. describe('Dom', () => {
  5. describe('css()', () => {
  6. describe('as getter', () => {
  7. it('returns all css as object', () => {
  8. const rect = new Rect({
  9. style: 'fill: none; outline-width: 1px; stroke: none'
  10. })
  11. expect(rect.css()).toEqual({
  12. fill: 'none',
  13. 'outline-width': '1px',
  14. stroke: 'none'
  15. })
  16. })
  17. it('returns an object with selected css properties', () => {
  18. const rect = new Rect({
  19. style: 'fill: none; outline-width: 1px; stroke: none'
  20. })
  21. expect(rect.css(['fill', 'stroke'])).toEqual({
  22. fill: 'none',
  23. stroke: 'none'
  24. })
  25. })
  26. it('returns a single property with property name given', () => {
  27. const rect = new Rect({
  28. style: 'fill: none; outline-width: 1px; stroke: none'
  29. })
  30. expect(rect.css('fill')).toBe('none')
  31. })
  32. it('correctly returns css vars', () => {
  33. const rect = new Rect({
  34. style: '--foo: red;'
  35. })
  36. expect(rect.css('--foo')).toBe('red')
  37. })
  38. it('returns undefined if css property is not set', () => {
  39. const rect = new Rect({
  40. style: 'fill: none; outline-width: 1px; stroke: none'
  41. })
  42. expect(rect.css('outline-color')).toBe('')
  43. })
  44. })
  45. describe('as setter', () => {
  46. it('returns itself', () => {
  47. const rect = new Rect({
  48. style: 'fill: none; outline-width: 1px; stroke: none'
  49. })
  50. expect(rect.css('fill', 'black')).toBe(rect)
  51. })
  52. it('adds a css property', () => {
  53. const rect = new Rect({
  54. style: 'fill: none; outline-width: 1px; stroke: none'
  55. })
  56. expect(rect.css('stroke-width', '2px').css('stroke-width')).toBe(
  57. '2px'
  58. )
  59. })
  60. it('changes a css property', () => {
  61. const rect = new Rect({
  62. style: 'fill: none; outline-width: 1px; stroke: none'
  63. })
  64. expect(rect.css('fill', 'black').css('fill')).toBe('black')
  65. })
  66. it('sets an object of properties', () => {
  67. const rect = new Rect()
  68. expect(rect.css({ fill: 'none', stroke: 'none' }).css()).toEqual({
  69. fill: 'none',
  70. stroke: 'none'
  71. })
  72. })
  73. it('removes property if empty string is passed as value', () => {
  74. const rect = new Rect({
  75. style: 'fill: none; outline-width: 1px; stroke: none'
  76. })
  77. expect(rect.css('fill', '').css('fill')).toBe('')
  78. })
  79. it('removes property if null is passed as value', () => {
  80. const rect = new Rect({
  81. style: 'fill: none; outline-width: 1px; stroke: none'
  82. })
  83. expect(rect.css('fill', null).css('fill')).toBe('')
  84. })
  85. it('removes property if null is passed as part of object', () => {
  86. const rect = new Rect({
  87. style: 'fill: none; outline-width: 1px; stroke: none'
  88. })
  89. expect(rect.css({ fill: null, stroke: 'black' }).css('fill')).toBe('')
  90. })
  91. it('allows single set of css vars', () => {
  92. const rect = new Rect().css('--foo', 'red').css('--foo', 'green')
  93. expect(rect.css()).toEqual({
  94. '--foo': 'green'
  95. })
  96. })
  97. it('allows multiple set of css vars via object', () => {
  98. const rect = new Rect().css({ '--foo': 'red', '--bar': 'green' })
  99. expect(rect.css()).toEqual({
  100. '--foo': 'red',
  101. '--bar': 'green'
  102. })
  103. })
  104. })
  105. })
  106. describe('show()', () => {
  107. it('returns itself', () => {
  108. const rect = new Rect()
  109. expect(rect.show()).toBe(rect)
  110. })
  111. it('removes the display property', () => {
  112. const rect = new Rect().hide()
  113. expect(rect.show().css('display')).toBe('')
  114. })
  115. })
  116. describe('hide()', () => {
  117. it('returns itself', () => {
  118. const rect = new Rect()
  119. expect(rect.hide()).toBe(rect)
  120. })
  121. it('sets the css display property to none', () => {
  122. const rect = new Rect()
  123. expect(rect.hide().css('display')).toBe('none')
  124. })
  125. })
  126. describe('visible()', () => {
  127. it('returns true if display is not none', () => {
  128. const rect = new Rect()
  129. expect(rect.show().visible()).toBe(true)
  130. expect(rect.css('display', 'block').visible()).toBe(true)
  131. })
  132. it('returns false if display is none', () => {
  133. const rect = new Rect()
  134. expect(rect.hide().visible()).toBe(false)
  135. })
  136. })
  137. })
  138. })