popconfirm.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { clear } from 'jest-date-mock';
  2. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  3. import Popconfirm from '../index';
  4. import { Button } from '../../index';
  5. import { genAfterEach, genBeforeEach, mount, sleep } from '../../_test_/utils';
  6. const wrapCls = `${BASE_CLASS_PREFIX}-popconfirm`;
  7. const wrapSelector = `.${BASE_CLASS_PREFIX}-popover .${wrapCls}`;
  8. const triggerCls = 'trigger';
  9. describe(`Popconfirm`, () => {
  10. beforeEach(() => {
  11. clear();
  12. genBeforeEach()();
  13. });
  14. afterEach(genAfterEach());
  15. it(`test appearance`, async () => {
  16. const elem = mount(
  17. <Popconfirm
  18. position="bottomLeft"
  19. title="确定是否要保存此修改?"
  20. content="此修改将不可逆"
  21. trigger={'click'}
  22. defaultVisible={false}
  23. >
  24. <Button className={triggerCls}>Save</Button>
  25. </Popconfirm>
  26. );
  27. // check if popconfirm hid or not
  28. expect(document.querySelectorAll(wrapSelector).length === 0).toBeTruthy();
  29. const triggerElem = document.querySelector(`.${triggerCls}`);
  30. triggerElem.click();
  31. // check if popconfirm showed or not
  32. expect(document.querySelectorAll(wrapSelector).length > 0).toBeTruthy();
  33. });
  34. it(`test disabled appearance`, async () => {
  35. const elem = mount(
  36. <Popconfirm
  37. position="bottomLeft"
  38. title="确定是否要保存此修改?"
  39. content="此修改将不可逆"
  40. trigger={'click'}
  41. defaultVisible={true}
  42. disabled={true}
  43. >
  44. <Button className={triggerCls}>Save</Button>
  45. </Popconfirm>
  46. );
  47. // check if popconfirm hid or not
  48. expect(document.querySelectorAll(wrapSelector).length === 0).toBeTruthy();
  49. });
  50. it(`test controlled appearance`, async () => {
  51. const props = { visible: true };
  52. const toggleShow = sinon.spy(() => {
  53. props.visible = !props.visible;
  54. elem.setProps(props);
  55. });
  56. const elem = mount(
  57. <Popconfirm
  58. position="bottomLeft"
  59. title="确定是否要保存此修改?"
  60. content="此修改将不可逆"
  61. {...props}
  62. trigger={'custom'}
  63. >
  64. <Button className={triggerCls} onClick={toggleShow}>
  65. Save
  66. </Button>
  67. </Popconfirm>
  68. );
  69. // check if popconfirm hid or not
  70. expect(document.querySelectorAll(wrapSelector).length > 0).toBeTruthy();
  71. // click button and hide popconfirm
  72. const triggerBtn = document.querySelector(`.${triggerCls}`);
  73. triggerBtn.click();
  74. await sleep(200);
  75. expect(toggleShow.called).toBeTruthy();
  76. expect(elem.prop('visible')).toBeFalsy();
  77. });
  78. it(`test buttons`, async () => {
  79. const onCancel = sinon.spy();
  80. const onConfirm = sinon.spy();
  81. const elem = mount(
  82. <Popconfirm
  83. position="bottomLeft"
  84. title="确定是否要保存此修改?"
  85. content="此修改将不可逆"
  86. trigger={'click'}
  87. onCancel={onCancel}
  88. onConfirm={onConfirm}
  89. defaultVisible={true}
  90. >
  91. <Button className={triggerCls}>Save</Button>
  92. </Popconfirm>
  93. );
  94. // check if popconfirm showed or not
  95. expect(document.querySelectorAll(wrapSelector).length > 0).toBeTruthy();
  96. // click cancel button and check if hid and trigger onCancel
  97. const buttons = document.querySelectorAll(`.${BASE_CLASS_PREFIX}-popconfirm-footer button`);
  98. const cancelButton = buttons[0];
  99. cancelButton.click();
  100. expect(onCancel.called).toBeTruthy();
  101. expect(elem.state('visible')).toBeFalsy();
  102. // click confirm button and check if showd and trigger onConfirm
  103. const confirmButton = buttons[1];
  104. elem.setState({ visible: true });
  105. confirmButton.click();
  106. expect(onConfirm.called).toBeTruthy();
  107. expect(elem.state('visible')).toBeFalsy();
  108. });
  109. });