styling.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { VirtualDOM, h } from '@lumino/virtualdom';
  5. import { simulate } from 'simulate-event';
  6. import { Styling } from '@jupyterlab/apputils';
  7. describe('@jupyterlab/apputils', () => {
  8. describe('Styling', () => {
  9. describe('.styleNode()', () => {
  10. it('should style descendant nodes for select, input and button', () => {
  11. const vnode = h.div({}, [h.button(), h.select(), h.input()]);
  12. const node = VirtualDOM.realize(vnode);
  13. Styling.styleNode(node);
  14. expect(node.querySelectorAll('.jp-mod-styled').length).toBe(3);
  15. });
  16. it('should wrap a select node', () => {
  17. const parent = document.createElement('div');
  18. const select = document.createElement('select');
  19. parent.appendChild(select);
  20. Styling.styleNode(parent);
  21. const wrapper = parent.firstChild as HTMLElement;
  22. expect(wrapper.className).toBe('jp-select-wrapper');
  23. expect(select.parentElement).toBe(wrapper);
  24. expect(select.className).toBe('jp-mod-styled');
  25. document.body.appendChild(parent);
  26. select.focus();
  27. simulate(select, 'focus');
  28. expect(wrapper.className).toContain('jp-mod-focused');
  29. select.blur();
  30. simulate(select, 'blur');
  31. expect(wrapper.className).not.toContain('jp-mod-focused');
  32. document.body.removeChild(parent);
  33. });
  34. });
  35. describe('.styleNodeByTag()', () => {
  36. it('should style descendant nodes for the given tag', () => {
  37. const vnode = h.div({}, [h.span(), h.div({}, h.span())]);
  38. const node = VirtualDOM.realize(vnode);
  39. Styling.styleNodeByTag(node, 'span');
  40. expect(node.querySelectorAll('.jp-mod-styled').length).toBe(2);
  41. });
  42. it('should style the node itself', () => {
  43. const div = document.createElement('div');
  44. Styling.styleNodeByTag(div, 'div');
  45. expect(div.className).toContain('jp-mod-styled');
  46. });
  47. });
  48. describe('.wrapSelect()', () => {
  49. it('should wrap the select node', () => {
  50. const select = document.createElement('select');
  51. const wrapper = Styling.wrapSelect(select);
  52. expect(wrapper.className).toBe('jp-select-wrapper');
  53. expect(select.parentElement).toBe(wrapper);
  54. expect(select.className).toBe('jp-mod-styled');
  55. document.body.appendChild(wrapper);
  56. select.focus();
  57. simulate(select, 'focus');
  58. expect(wrapper.className).toContain('jp-mod-focused');
  59. select.blur();
  60. simulate(select, 'blur');
  61. expect(wrapper.className).not.toContain('jp-mod-focused');
  62. document.body.removeChild(wrapper);
  63. });
  64. });
  65. });
  66. });