styling.spec.ts 2.7 KB

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