editor-panel-spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const EditorPanel = require('../lib/editor-panel');
  2. describe("EditorPanel", function() {
  3. let panel = null;
  4. const getValueForId = function(id) {
  5. const element = panel.element.querySelector(`#${id.replace(/\./g, '\\.')}`);
  6. if (element?.tagName === "INPUT") {
  7. return element.checked;
  8. } else if (element?.tagName === "SELECT") {
  9. return element.value;
  10. } else if (element != null) {
  11. return element.getModel().getText();
  12. } else {
  13. return;
  14. }
  15. };
  16. const setValueForId = function(id, value) {
  17. const element = panel.element.querySelector(`#${id.replace(/\./g, '\\.')}`);
  18. if (element.tagName === "INPUT") {
  19. element.checked = value;
  20. return element.dispatchEvent(new Event('change', {bubbles: true}));
  21. } else if (element.tagName === "SELECT") {
  22. element.value = value;
  23. return element.dispatchEvent(new Event('change', {bubbles: true}));
  24. } else {
  25. element.getModel().setText(value?.toString());
  26. return window.advanceClock(10000); // wait for contents-modified to be triggered
  27. }
  28. };
  29. beforeEach(function() {
  30. atom.config.set('editor.boolean', true);
  31. atom.config.set('editor.string', 'hey');
  32. atom.config.set('editor.object', {boolean: true, int: 3, string: 'test'});
  33. atom.config.set('editor.simpleArray', ['a', 'b', 'c']);
  34. atom.config.set('editor.complexArray', ['a', 'b', {c: true}]);
  35. atom.config.setSchema('', {type: 'object'});
  36. return panel = new EditorPanel();
  37. });
  38. it("automatically binds named fields to their corresponding config keys", function() {
  39. expect(getValueForId('editor.boolean')).toBeTruthy();
  40. expect(getValueForId('editor.string')).toBe('hey');
  41. expect(getValueForId('editor.object.boolean')).toBeTruthy();
  42. expect(getValueForId('editor.object.int')).toBe('3');
  43. expect(getValueForId('editor.object.string')).toBe('test');
  44. atom.config.set('editor.boolean', false);
  45. atom.config.set('editor.string', 'hey again');
  46. atom.config.set('editor.object.boolean', false);
  47. atom.config.set('editor.object.int', 6);
  48. atom.config.set('editor.object.string', 'hi');
  49. expect(getValueForId('editor.boolean')).toBeFalsy();
  50. expect(getValueForId('editor.string')).toBe('hey again');
  51. expect(getValueForId('editor.object.boolean')).toBeFalsy();
  52. expect(getValueForId('editor.object.int')).toBe('6');
  53. expect(getValueForId('editor.object.string')).toBe('hi');
  54. setValueForId('editor.string', "oh hi");
  55. setValueForId('editor.boolean', true);
  56. setValueForId('editor.object.boolean', true);
  57. setValueForId('editor.object.int', 9);
  58. setValueForId('editor.object.string', 'yo');
  59. expect(atom.config.get('editor.boolean')).toBe(true);
  60. expect(atom.config.get('editor.string')).toBe('oh hi');
  61. expect(atom.config.get('editor.object.boolean')).toBe(true);
  62. expect(atom.config.get('editor.object.int')).toBe(9);
  63. expect(atom.config.get('editor.object.string')).toBe('yo');
  64. setValueForId('editor.string', '');
  65. setValueForId('editor.object.int', '');
  66. setValueForId('editor.object.string', '');
  67. expect(atom.config.get('editor.string')).toBeUndefined();
  68. expect(atom.config.get('editor.object.int')).toBeUndefined();
  69. expect(atom.config.get('editor.object.string')).toBeUndefined();
  70. });
  71. it("does not save the config value until it has been changed to a new value", function() {
  72. const observeHandler = jasmine.createSpy("observeHandler");
  73. atom.config.observe("editor.simpleArray", observeHandler);
  74. observeHandler.reset();
  75. window.advanceClock(10000); // wait for contents-modified to be triggered
  76. expect(observeHandler).not.toHaveBeenCalled();
  77. setValueForId('editor.simpleArray', 2);
  78. expect(observeHandler).toHaveBeenCalled();
  79. observeHandler.reset();
  80. setValueForId('editor.simpleArray', 2);
  81. expect(observeHandler).not.toHaveBeenCalled();
  82. });
  83. it("does not update the editor text unless the value it parses to changes", function() {
  84. setValueForId('editor.simpleArray', "a, b,");
  85. expect(atom.config.get('editor.simpleArray')).toEqual(['a', 'b']);
  86. expect(getValueForId('editor.simpleArray')).toBe('a, b,');
  87. });
  88. it("only adds editors for arrays when all the values in the array are strings", function() {
  89. expect(getValueForId('editor.simpleArray')).toBe('a, b, c');
  90. expect(getValueForId('editor.complexArray')).toBeUndefined();
  91. setValueForId('editor.simpleArray', 'a, d');
  92. expect(atom.config.get('editor.simpleArray')).toEqual(['a', 'd']);
  93. expect(atom.config.get('editor.complexArray')).toEqual(['a', 'b', {c: true}]);
  94. });
  95. it("shows the package settings notes for core and editor settings", function() {
  96. expect(panel.element.querySelector('#editor-settings-note')).toExist();
  97. expect(panel.element.querySelector('#editor-settings-note').textContent).toContain('Check language settings');
  98. });
  99. });