toast.test.js 4.5 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.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. };
  37. Toast.info(opts);
  38. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  39. expect(toast.textContent).toEqual('dur1');
  40. setTimeout(() => {
  41. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`); // expect toast had been remove
  42. expect(toast).toEqual(null);
  43. done();
  44. }, 1500);
  45. });
  46. it('update content by id', done => {
  47. const id = 'toastid'
  48. Toast.info({ content: 'bytedance', id });
  49. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  50. expect(toast.textContent).toEqual('bytedance');
  51. setTimeout(() => {
  52. Toast.info({ content: 'dancebyte', id });
  53. expect(toast.textContent).toEqual('dancebyte');
  54. setTimeout(() => {
  55. Toast.error({ content: 'error', id });
  56. expect(toast.textContent).toEqual('error');
  57. expect(toast?.className).toEqual(`${BASE_CLASS_PREFIX}-toast ${BASE_CLASS_PREFIX}-toast-error`)
  58. done()
  59. }, 1000)
  60. }, 1000)
  61. });
  62. it('should trigger onClose after duration', done => {
  63. let spyOnClose = sinon.spy(() => {});
  64. let opts = {
  65. onClose: spyOnClose,
  66. content: 'bytedance',
  67. duration: 0.8,
  68. };
  69. Toast.info(opts);
  70. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  71. setTimeout(() => {
  72. expect(spyOnClose.calledOnce).toEqual(true);
  73. done();
  74. }, 1000);
  75. });
  76. it('should trigger onClose when click close btn', () => {
  77. let spyOnClose = sinon.spy(() => {});
  78. let opts = {
  79. onClose: spyOnClose,
  80. content: 'bytedance',
  81. };
  82. let toast = mount(<ToastItem {...opts} />);
  83. let closeBtn = toast.find(`.${BASE_CLASS_PREFIX}-toast-close-button .${BASE_CLASS_PREFIX}-button`);
  84. closeBtn.simulate('click', {});
  85. expect(spyOnClose.calledOnce).toEqual(true);
  86. });
  87. it('showClear = false', () => {
  88. let opts = {
  89. showClose: false,
  90. duration: 1,
  91. content: 'showCloseToast',
  92. };
  93. let toast = mount(<ToastItem {...opts} />);
  94. expect(toast.exists(`.${BASE_CLASS_PREFIX}-toast-close-button`)).toEqual(false);
  95. });
  96. it('custom icon', () => {
  97. let customIcon = <IconWifi />;
  98. let opts = {
  99. icon: customIcon,
  100. content: 'semi',
  101. };
  102. let toast = mount(<ToastItem {...opts} />);
  103. expect(toast.exists(`.${BASE_CLASS_PREFIX}-icon-wifi`)).toEqual(true);
  104. });
  105. it('specific getPopupContainer', () => {
  106. const container = document.createElement('div');
  107. container.id = 'toast-container';
  108. document.body.appendChild(container);
  109. const ContainerToast = ToastFactory.create({
  110. getPopupContainer: () => document.querySelector(`#toast-container`),
  111. });
  112. ContainerToast.info('specific getPopupContainer');
  113. expect(
  114. document.querySelector(`#${ContainerToast.getWrapperId()}`).parentNode ===
  115. document.querySelector(`#toast-container`)
  116. ).toEqual(true);
  117. });
  118. });