toast.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Toast, Icon, Button, ToastFactory } from '../../index';
  2. import { advanceBy, advanceTo, clear } from 'jest-date-mock';
  3. import ToastItem from '../toast';
  4. import sleep from '@douyinfe/semi-ui/_test_/utils/function/sleep';
  5. import { BASE_CLASS_PREFIX } from '../../../semi-foundation/base/constants';
  6. import { IconWifi } from '@douyinfe/semi-icons';
  7. describe('Toast', () => {
  8. beforeEach(async () => {
  9. // semi-animation会使用Date.now(),所以这里需要clear掉全局的advanceTo对Date的修改
  10. clear();
  11. const tw = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-wrapper`);
  12. if (tw) {
  13. tw.childNodes[0].innerHTML = '';
  14. }
  15. });
  16. afterEach(async () => {
  17. // await sleep(500);
  18. });
  19. it('basically show toast, test content', () => {
  20. Toast.info('semi');
  21. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  22. expect(toast.textContent).toEqual('semi');
  23. });
  24. it('theme: light', () => {
  25. Toast.info({
  26. content: 'semi',
  27. theme: 'light',
  28. });
  29. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  30. expect(toast.classList.contains(`${BASE_CLASS_PREFIX}-toast-light`)).toEqual(true);
  31. });
  32. it('duration, >0', done => {
  33. let opts = {
  34. duration: 1,
  35. content: 'dur1',
  36. motion:false,
  37. };
  38. Toast.info(opts);
  39. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  40. expect(toast.textContent).toEqual('dur1');
  41. setTimeout(() => {
  42. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`); // expect toast had been remove
  43. expect(toast).toEqual(null);
  44. done();
  45. }, 1500);
  46. });
  47. it('update content by id', done => {
  48. const id = 'toastid'
  49. Toast.info({ content: 'bytedance', id, motion: false });
  50. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  51. expect(toast.textContent).toEqual('bytedance');
  52. setTimeout(() => {
  53. Toast.info({ content: 'dancebyte', id, motion: false });
  54. expect(toast.textContent).toEqual('dancebyte');
  55. setTimeout(() => {
  56. Toast.error({ content: 'error', id, motion: false});
  57. expect(toast.textContent).toEqual('error');
  58. expect(toast?.className).toEqual(`${BASE_CLASS_PREFIX}-toast ${BASE_CLASS_PREFIX}-toast-error`)
  59. done()
  60. }, 1000)
  61. }, 1000)
  62. });
  63. it('should trigger onClose after duration', done => {
  64. let spyOnClose = sinon.spy(() => {});
  65. let opts = {
  66. onClose: spyOnClose,
  67. content: 'bytedance',
  68. duration: 0.8,
  69. };
  70. Toast.info(opts);
  71. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  72. setTimeout(() => {
  73. expect(spyOnClose.calledOnce).toEqual(true);
  74. done();
  75. }, 1000);
  76. });
  77. it('should trigger onClose when click close btn', () => {
  78. let spyOnClose = sinon.spy(() => {});
  79. let opts = {
  80. onClose: spyOnClose,
  81. content: 'bytedance',
  82. };
  83. let toast = mount(<ToastItem {...opts} />);
  84. let closeBtn = toast.find(`.${BASE_CLASS_PREFIX}-toast-close-button .${BASE_CLASS_PREFIX}-button`);
  85. closeBtn.simulate('click', {});
  86. expect(spyOnClose.calledOnce).toEqual(true);
  87. });
  88. it('showClear = false', () => {
  89. let opts = {
  90. showClose: false,
  91. duration: 1,
  92. content: 'showCloseToast',
  93. };
  94. let toast = mount(<ToastItem {...opts} />);
  95. expect(toast.exists(`.${BASE_CLASS_PREFIX}-toast-close-button`)).toEqual(false);
  96. });
  97. it('custom icon', () => {
  98. let customIcon = <IconWifi />;
  99. let opts = {
  100. icon: customIcon,
  101. content: 'semi',
  102. };
  103. let toast = mount(<ToastItem {...opts} />);
  104. expect(toast.exists(`.${BASE_CLASS_PREFIX}-icon-wifi`)).toEqual(true);
  105. });
  106. it('specific getPopupContainer', () => {
  107. const container = document.createElement('div');
  108. container.id = 'toast-container';
  109. document.body.appendChild(container);
  110. const ContainerToast = ToastFactory.create({
  111. getPopupContainer: () => document.querySelector(`#toast-container`),
  112. });
  113. ContainerToast.info('specific getPopupContainer');
  114. expect(
  115. document.querySelector(`#${ContainerToast.getWrapperId()}`).parentNode ===
  116. document.querySelector(`#toast-container`)
  117. ).toEqual(true);
  118. });
  119. });