styles-element-spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const { createStylesElement } = require('../src/styles-element');
  2. describe('StylesElement', function() {
  3. let [
  4. element,
  5. addedStyleElements,
  6. removedStyleElements,
  7. updatedStyleElements
  8. ] = [];
  9. beforeEach(function() {
  10. element = createStylesElement();
  11. element.initialize(atom.styles);
  12. document.querySelector('#jasmine-content').appendChild(element);
  13. addedStyleElements = [];
  14. removedStyleElements = [];
  15. updatedStyleElements = [];
  16. element.onDidAddStyleElement(element => addedStyleElements.push(element));
  17. element.onDidRemoveStyleElement(element =>
  18. removedStyleElements.push(element)
  19. );
  20. element.onDidUpdateStyleElement(element =>
  21. updatedStyleElements.push(element)
  22. );
  23. });
  24. it('renders a style tag for all currently active stylesheets in the style manager', function() {
  25. const initialChildCount = element.children.length;
  26. const disposable1 = atom.styles.addStyleSheet('a {color: red;}');
  27. expect(element.children.length).toBe(initialChildCount + 1);
  28. expect(element.children[initialChildCount].textContent).toBe(
  29. 'a {color: red;}'
  30. );
  31. expect(addedStyleElements).toEqual([element.children[initialChildCount]]);
  32. atom.styles.addStyleSheet('a {color: blue;}');
  33. expect(element.children.length).toBe(initialChildCount + 2);
  34. expect(element.children[initialChildCount + 1].textContent).toBe(
  35. 'a {color: blue;}'
  36. );
  37. expect(addedStyleElements).toEqual([
  38. element.children[initialChildCount],
  39. element.children[initialChildCount + 1]
  40. ]);
  41. disposable1.dispose();
  42. expect(element.children.length).toBe(initialChildCount + 1);
  43. expect(element.children[initialChildCount].textContent).toBe(
  44. 'a {color: blue;}'
  45. );
  46. expect(removedStyleElements).toEqual([addedStyleElements[0]]);
  47. });
  48. it('orders style elements by priority', function() {
  49. const initialChildCount = element.children.length;
  50. atom.styles.addStyleSheet('a {color: red}', { priority: 1 });
  51. atom.styles.addStyleSheet('a {color: blue}', { priority: 0 });
  52. atom.styles.addStyleSheet('a {color: green}', { priority: 2 });
  53. atom.styles.addStyleSheet('a {color: yellow}', { priority: 1 });
  54. expect(element.children[initialChildCount].textContent).toBe(
  55. 'a {color: blue}'
  56. );
  57. expect(element.children[initialChildCount + 1].textContent).toBe(
  58. 'a {color: red}'
  59. );
  60. expect(element.children[initialChildCount + 2].textContent).toBe(
  61. 'a {color: yellow}'
  62. );
  63. expect(element.children[initialChildCount + 3].textContent).toBe(
  64. 'a {color: green}'
  65. );
  66. });
  67. it('updates existing style nodes when style elements are updated', function() {
  68. const initialChildCount = element.children.length;
  69. atom.styles.addStyleSheet('a {color: red;}', { sourcePath: '/foo/bar' });
  70. atom.styles.addStyleSheet('a {color: blue;}', { sourcePath: '/foo/bar' });
  71. expect(element.children.length).toBe(initialChildCount + 1);
  72. expect(element.children[initialChildCount].textContent).toBe(
  73. 'a {color: blue;}'
  74. );
  75. expect(updatedStyleElements).toEqual([element.children[initialChildCount]]);
  76. });
  77. it("only includes style elements matching the 'context' attribute", function() {
  78. const initialChildCount = element.children.length;
  79. atom.styles.addStyleSheet('a {color: red;}', { context: 'test-context' });
  80. atom.styles.addStyleSheet('a {color: green;}');
  81. expect(element.children.length).toBe(initialChildCount + 2);
  82. expect(element.children[initialChildCount].textContent).toBe(
  83. 'a {color: red;}'
  84. );
  85. expect(element.children[initialChildCount + 1].textContent).toBe(
  86. 'a {color: green;}'
  87. );
  88. element.setAttribute('context', 'test-context');
  89. expect(element.children.length).toBe(1);
  90. expect(element.children[0].textContent).toBe('a {color: red;}');
  91. atom.styles.addStyleSheet('a {color: blue;}', { context: 'test-context' });
  92. atom.styles.addStyleSheet('a {color: yellow;}');
  93. expect(element.children.length).toBe(2);
  94. expect(element.children[0].textContent).toBe('a {color: red;}');
  95. expect(element.children[1].textContent).toBe('a {color: blue;}');
  96. });
  97. });