tabbar.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { createRouter, createWebHistory } from 'vue-router';
  2. import { createPinia, setActivePinia } from 'pinia';
  3. import { beforeEach, describe, expect, it, vi } from 'vitest';
  4. import { useTabbarStore } from './tabbar';
  5. describe('useAccessStore', () => {
  6. const router = createRouter({
  7. history: createWebHistory(),
  8. routes: [],
  9. });
  10. router.push = vi.fn();
  11. router.replace = vi.fn();
  12. beforeEach(() => {
  13. setActivePinia(createPinia());
  14. vi.clearAllMocks();
  15. });
  16. it('adds a new tab', () => {
  17. const store = useTabbarStore();
  18. const tab: any = {
  19. fullPath: '/home',
  20. meta: {},
  21. name: 'Home',
  22. path: '/home',
  23. };
  24. store.addTab(tab);
  25. expect(store.tabs.length).toBe(1);
  26. expect(store.tabs[0]).toEqual(tab);
  27. });
  28. it('adds a new tab if it does not exist', () => {
  29. const store = useTabbarStore();
  30. const newTab: any = {
  31. fullPath: '/new',
  32. meta: {},
  33. name: 'New',
  34. path: '/new',
  35. };
  36. store.addTab(newTab);
  37. expect(store.tabs).toContainEqual(newTab);
  38. });
  39. it('updates an existing tab instead of adding a new one', () => {
  40. const store = useTabbarStore();
  41. const initialTab: any = {
  42. fullPath: '/existing',
  43. meta: {},
  44. name: 'Existing',
  45. path: '/existing',
  46. query: {},
  47. };
  48. store.tabs.push(initialTab);
  49. const updatedTab = { ...initialTab, query: { id: '1' } };
  50. store.addTab(updatedTab);
  51. expect(store.tabs.length).toBe(1);
  52. expect(store.tabs[0].query).toEqual({ id: '1' });
  53. });
  54. it('closes all tabs', async () => {
  55. const store = useTabbarStore();
  56. store.tabs = [
  57. { fullPath: '/home', meta: {}, name: 'Home', path: '/home' },
  58. ] as any;
  59. router.replace = vi.fn(); // 使用 vitest 的 mock 函数
  60. await store.closeAllTabs(router);
  61. expect(store.tabs.length).toBe(0); // 假设没有固定的标签页
  62. // expect(router.replace).toHaveBeenCalled();
  63. });
  64. it('closes a non-affix tab', () => {
  65. const store = useTabbarStore();
  66. const tab: any = {
  67. fullPath: '/closable',
  68. meta: {},
  69. name: 'Closable',
  70. path: '/closable',
  71. };
  72. store.tabs.push(tab);
  73. store._close(tab);
  74. expect(store.tabs.length).toBe(0);
  75. });
  76. it('does not close an affix tab', () => {
  77. const store = useTabbarStore();
  78. const affixTab: any = {
  79. fullPath: '/affix',
  80. meta: { affixTab: true },
  81. name: 'Affix',
  82. path: '/affix',
  83. };
  84. store.tabs.push(affixTab);
  85. store._close(affixTab);
  86. expect(store.tabs.length).toBe(1); // Affix tab should not be closed
  87. });
  88. it('returns all cache tabs', () => {
  89. const store = useTabbarStore();
  90. store.cachedTabs.add('Home');
  91. store.cachedTabs.add('About');
  92. expect(store.getCachedTabs).toEqual(['Home', 'About']);
  93. });
  94. it('returns all tabs, including affix tabs', () => {
  95. const store = useTabbarStore();
  96. const normalTab: any = {
  97. fullPath: '/normal',
  98. meta: {},
  99. name: 'Normal',
  100. path: '/normal',
  101. };
  102. const affixTab: any = {
  103. fullPath: '/affix',
  104. meta: { affixTab: true },
  105. name: 'Affix',
  106. path: '/affix',
  107. };
  108. store.tabs.push(normalTab);
  109. store.affixTabs.push(affixTab);
  110. expect(store.getTabs).toContainEqual(normalTab);
  111. expect(store.affixTabs).toContainEqual(affixTab);
  112. });
  113. it('navigates to a specific tab', async () => {
  114. const store = useTabbarStore();
  115. const tab: any = { meta: {}, name: 'Dashboard', path: '/dashboard' };
  116. await store._goToTab(tab, router);
  117. expect(router.replace).toHaveBeenCalledWith({
  118. params: {},
  119. path: '/dashboard',
  120. query: {},
  121. });
  122. });
  123. it('closes multiple tabs by paths', async () => {
  124. const store = useTabbarStore();
  125. store.addTab({
  126. fullPath: '/home',
  127. meta: {},
  128. name: 'Home',
  129. path: '/home',
  130. } as any);
  131. store.addTab({
  132. fullPath: '/about',
  133. meta: {},
  134. name: 'About',
  135. path: '/about',
  136. } as any);
  137. store.addTab({
  138. fullPath: '/contact',
  139. meta: {},
  140. name: 'Contact',
  141. path: '/contact',
  142. } as any);
  143. await store._bulkCloseByPaths(['/home', '/contact']);
  144. expect(store.tabs).toHaveLength(1);
  145. expect(store.tabs[0].name).toBe('About');
  146. });
  147. it('closes all tabs to the left of the specified tab', async () => {
  148. const store = useTabbarStore();
  149. store.addTab({
  150. fullPath: '/home',
  151. meta: {},
  152. name: 'Home',
  153. path: '/home',
  154. } as any);
  155. store.addTab({
  156. fullPath: '/about',
  157. meta: {},
  158. name: 'About',
  159. path: '/about',
  160. } as any);
  161. const targetTab: any = {
  162. fullPath: '/contact',
  163. meta: {},
  164. name: 'Contact',
  165. path: '/contact',
  166. };
  167. store.addTab(targetTab);
  168. await store.closeLeftTabs(targetTab);
  169. expect(store.tabs).toHaveLength(1);
  170. expect(store.tabs[0].name).toBe('Contact');
  171. });
  172. it('closes all tabs except the specified tab', async () => {
  173. const store = useTabbarStore();
  174. store.addTab({
  175. fullPath: '/home',
  176. meta: {},
  177. name: 'Home',
  178. path: '/home',
  179. } as any);
  180. const targetTab: any = {
  181. fullPath: '/about',
  182. meta: {},
  183. name: 'About',
  184. path: '/about',
  185. };
  186. store.addTab(targetTab);
  187. store.addTab({
  188. fullPath: '/contact',
  189. meta: {},
  190. name: 'Contact',
  191. path: '/contact',
  192. } as any);
  193. await store.closeOtherTabs(targetTab);
  194. expect(store.tabs).toHaveLength(1);
  195. expect(store.tabs[0].name).toBe('About');
  196. });
  197. it('closes all tabs to the right of the specified tab', async () => {
  198. const store = useTabbarStore();
  199. const targetTab: any = {
  200. fullPath: '/home',
  201. meta: {},
  202. name: 'Home',
  203. path: '/home',
  204. };
  205. store.addTab(targetTab);
  206. store.addTab({
  207. fullPath: '/about',
  208. meta: {},
  209. name: 'About',
  210. path: '/about',
  211. } as any);
  212. store.addTab({
  213. fullPath: '/contact',
  214. meta: {},
  215. name: 'Contact',
  216. path: '/contact',
  217. } as any);
  218. await store.closeRightTabs(targetTab);
  219. expect(store.tabs).toHaveLength(1);
  220. expect(store.tabs[0].name).toBe('Home');
  221. });
  222. it('closes the tab with the specified key', async () => {
  223. const store = useTabbarStore();
  224. const keyToClose = '/about';
  225. store.addTab({
  226. fullPath: '/home',
  227. meta: {},
  228. name: 'Home',
  229. path: '/home',
  230. } as any);
  231. store.addTab({
  232. fullPath: keyToClose,
  233. meta: {},
  234. name: 'About',
  235. path: '/about',
  236. } as any);
  237. store.addTab({
  238. fullPath: '/contact',
  239. meta: {},
  240. name: 'Contact',
  241. path: '/contact',
  242. } as any);
  243. await store.closeTabByKey(keyToClose, router);
  244. expect(store.tabs).toHaveLength(2);
  245. expect(
  246. store.tabs.find((tab) => tab.fullPath === keyToClose),
  247. ).toBeUndefined();
  248. });
  249. it('refreshes the current tab', async () => {
  250. const store = useTabbarStore();
  251. const currentTab: any = {
  252. fullPath: '/dashboard',
  253. meta: { name: 'Dashboard' },
  254. name: 'Dashboard',
  255. path: '/dashboard',
  256. };
  257. router.currentRoute.value = currentTab;
  258. await store.refresh(router);
  259. expect(store.excludeCachedTabs.has('Dashboard')).toBe(false);
  260. expect(store.renderRouteView).toBe(true);
  261. });
  262. });