toast.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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('should trigger onClose after duration', done => {
  47. let spyOnClose = sinon.spy(() => {});
  48. let opts = {
  49. onClose: spyOnClose,
  50. content: 'bytedance',
  51. duration: 0.8,
  52. };
  53. Toast.info(opts);
  54. let toast = document.querySelector(`.${BASE_CLASS_PREFIX}-toast-info`);
  55. setTimeout(() => {
  56. expect(spyOnClose.calledOnce).toEqual(true);
  57. done();
  58. }, 1000);
  59. });
  60. it('should trigger onClose when click close btn', () => {
  61. let spyOnClose = sinon.spy(() => {});
  62. let opts = {
  63. onClose: spyOnClose,
  64. content: 'bytedance',
  65. };
  66. let toast = mount(<ToastItem {...opts} />);
  67. let closeBtn = toast.find(`.${BASE_CLASS_PREFIX}-toast-close-button .${BASE_CLASS_PREFIX}-button`);
  68. closeBtn.simulate('click', {});
  69. expect(spyOnClose.calledOnce).toEqual(true);
  70. });
  71. it('showClear = false', () => {
  72. let opts = {
  73. showClose: false,
  74. duration: 1,
  75. content: 'showCloseToast',
  76. };
  77. let toast = mount(<ToastItem {...opts} />);
  78. expect(toast.exists(`.${BASE_CLASS_PREFIX}-toast-close-button`)).toEqual(false);
  79. });
  80. it('custom icon', () => {
  81. let customIcon = <IconWifi />;
  82. let opts = {
  83. icon: customIcon,
  84. content: 'semi',
  85. };
  86. let toast = mount(<ToastItem {...opts} />);
  87. expect(toast.exists(`.${BASE_CLASS_PREFIX}-icon-wifi`)).toEqual(true);
  88. });
  89. it('specific getPopupContainer', () => {
  90. const container = document.createElement('div');
  91. container.id = 'toast-container';
  92. document.body.appendChild(container);
  93. const ContainerToast = ToastFactory.create({
  94. getPopupContainer: () => document.querySelector(`#toast-container`),
  95. });
  96. ContainerToast.info('specific getPopupContainer');
  97. expect(
  98. document.querySelector(`#${ContainerToast.getWrapperId()}`).parentNode ===
  99. document.querySelector(`#toast-container`)
  100. ).toEqual(true);
  101. });
  102. });