organizationSettingsForm.spec.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import {saveOnBlurUndoMessage} from 'app/actionCreators/indicator';
  4. import OrganizationSettingsForm from 'app/views/settings/organizationGeneralSettings/organizationSettingsForm';
  5. jest.mock('jquery');
  6. jest.mock('app/actionCreators/indicator');
  7. describe('OrganizationSettingsForm', function() {
  8. const organization = TestStubs.Organization();
  9. let putMock;
  10. const onSave = jest.fn();
  11. beforeEach(function() {
  12. MockApiClient.clearMockResponses();
  13. onSave.mockReset();
  14. });
  15. it('can change a form field', function(done) {
  16. putMock = MockApiClient.addMockResponse({
  17. url: '/organizations/3/',
  18. method: 'PUT',
  19. data: {
  20. name: 'New Name',
  21. },
  22. });
  23. const wrapper = mount(
  24. <OrganizationSettingsForm
  25. location={TestStubs.location()}
  26. orgId={organization.id}
  27. access={new Set('org:admin')}
  28. initialData={TestStubs.Organization()}
  29. onSave={onSave}
  30. />,
  31. TestStubs.routerContext()
  32. );
  33. const input = wrapper.find('input[name="name"]');
  34. expect(input).toHaveLength(1);
  35. input.simulate('change', {target: {value: 'New Name'}});
  36. input.simulate('blur');
  37. expect(putMock).toHaveBeenCalledWith(
  38. '/organizations/3/',
  39. expect.objectContaining({
  40. method: 'PUT',
  41. data: {
  42. name: 'New Name',
  43. },
  44. })
  45. );
  46. saveOnBlurUndoMessage.mockImplementationOnce(async function(
  47. change,
  48. model,
  49. fieldName
  50. ) {
  51. try {
  52. expect(fieldName).toBe('name');
  53. expect(change.old).toBe('Organization Name');
  54. expect(change.new).toBe('New Name');
  55. // Test "undo" call undo directly
  56. expect(model.getValue('name')).toBe('New Name');
  57. model.undo();
  58. expect(model.getValue('name')).toBe('Organization Name');
  59. // `saveOnBlurUndoMessage` saves the new field, so reimplement this
  60. await model.saveField('name', 'Organization Name');
  61. // Initial data should be updated to original name
  62. expect(model.initialData.name).toBe('Organization Name');
  63. putMock.mockReset();
  64. // Blurring the name field again should NOT trigger a save
  65. input.simulate('blur');
  66. expect(putMock).not.toHaveBeenCalled();
  67. done();
  68. } catch (err) {
  69. done(err);
  70. }
  71. });
  72. });
  73. it('can change slug', function() {
  74. putMock = MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/',
  76. method: 'PUT',
  77. });
  78. const wrapper = mount(
  79. <OrganizationSettingsForm
  80. location={TestStubs.location()}
  81. orgId={organization.slug}
  82. access={new Set('org:admin')}
  83. initialData={TestStubs.Organization()}
  84. onSave={onSave}
  85. />,
  86. TestStubs.routerContext()
  87. );
  88. wrapper
  89. .find('input[name="slug"]')
  90. .simulate('change', {target: {value: 'NEW SLUG'}})
  91. .simulate('blur');
  92. expect(putMock).not.toHaveBeenCalled();
  93. wrapper.find('SaveButton').simulate('click');
  94. expect(putMock).toHaveBeenCalledWith(
  95. '/organizations/org-slug/',
  96. expect.objectContaining({
  97. data: {
  98. slug: 'new-slug',
  99. },
  100. })
  101. );
  102. });
  103. });