sidepanel-hotmail-manager.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. test('sidepanel loads hotmail manager before sidepanel bootstrap', () => {
  5. const html = fs.readFileSync('sidepanel/sidepanel.html', 'utf8');
  6. const hotmailManagerIndex = html.indexOf('<script src="hotmail-manager.js"></script>');
  7. const sidepanelIndex = html.indexOf('<script src="sidepanel.js"></script>');
  8. assert.notEqual(hotmailManagerIndex, -1);
  9. assert.notEqual(sidepanelIndex, -1);
  10. assert.ok(hotmailManagerIndex < sidepanelIndex);
  11. });
  12. test('hotmail manager exposes a factory and renders empty state', () => {
  13. const source = fs.readFileSync('sidepanel/hotmail-manager.js', 'utf8');
  14. const windowObject = {};
  15. const localStorageMock = {
  16. getItem() {
  17. return null;
  18. },
  19. setItem() {},
  20. };
  21. const api = new Function('window', 'localStorage', `${source}; return window.SidepanelHotmailManager;`)(
  22. windowObject,
  23. localStorageMock
  24. );
  25. assert.equal(typeof api?.createHotmailManager, 'function');
  26. const hotmailAccountsList = { innerHTML: '' };
  27. const toggleButton = {
  28. textContent: '',
  29. disabled: false,
  30. setAttribute() {},
  31. };
  32. const noopClassList = { toggle() {} };
  33. const manager = api.createHotmailManager({
  34. state: {
  35. getLatestState: () => ({ currentHotmailAccountId: null }),
  36. syncLatestState() {},
  37. },
  38. dom: {
  39. btnClearUsedHotmailAccounts: { textContent: '', disabled: false },
  40. btnDeleteAllHotmailAccounts: { textContent: '', disabled: false },
  41. btnToggleHotmailList: toggleButton,
  42. hotmailAccountsList,
  43. hotmailListShell: { classList: noopClassList },
  44. selectMailProvider: { value: 'hotmail-api' },
  45. inputEmail: { value: '' },
  46. },
  47. helpers: {
  48. getHotmailAccounts: () => [],
  49. getCurrentHotmailEmail: () => '',
  50. escapeHtml: (value) => String(value || ''),
  51. showToast() {},
  52. openConfirmModal: async () => true,
  53. copyTextToClipboard: async () => {},
  54. },
  55. runtime: {
  56. sendMessage: async () => ({}),
  57. },
  58. constants: {
  59. copyIcon: '',
  60. displayTimeZone: 'Asia/Shanghai',
  61. expandedStorageKey: 'multipage-hotmail-list-expanded',
  62. },
  63. hotmailUtils: {},
  64. });
  65. assert.equal(typeof manager.renderHotmailAccounts, 'function');
  66. assert.equal(typeof manager.bindHotmailEvents, 'function');
  67. assert.equal(typeof manager.initHotmailListExpandedState, 'function');
  68. manager.renderHotmailAccounts();
  69. assert.match(hotmailAccountsList.innerHTML, /还没有 Hotmail 账号/);
  70. });