tabbar.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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();
  60. await store.closeAllTabs(router);
  61. expect(store.tabs.length).toBe(1);
  62. });
  63. it('closes a non-affix tab', () => {
  64. const store = useTabbarStore();
  65. const tab: any = {
  66. fullPath: '/closable',
  67. meta: {},
  68. name: 'Closable',
  69. path: '/closable',
  70. };
  71. store.tabs.push(tab);
  72. store._close(tab);
  73. expect(store.tabs.length).toBe(0);
  74. });
  75. it('does not close an affix tab', () => {
  76. const store = useTabbarStore();
  77. const affixTab: any = {
  78. fullPath: '/affix',
  79. meta: { affixTab: true },
  80. name: 'Affix',
  81. path: '/affix',
  82. };
  83. store.tabs.push(affixTab);
  84. store._close(affixTab);
  85. expect(store.tabs.length).toBe(1); // Affix tab should not be closed
  86. });
  87. it('returns all cache tabs', () => {
  88. const store = useTabbarStore();
  89. store.cachedTabs.add('Home');
  90. store.cachedTabs.add('About');
  91. expect(store.getCachedTabs).toEqual(['Home', 'About']);
  92. });
  93. it('returns all tabs, including affix tabs', () => {
  94. const store = useTabbarStore();
  95. const normalTab: any = {
  96. fullPath: '/normal',
  97. meta: {},
  98. name: 'Normal',
  99. path: '/normal',
  100. };
  101. const affixTab: any = {
  102. fullPath: '/affix',
  103. meta: { affixTab: true },
  104. name: 'Affix',
  105. path: '/affix',
  106. };
  107. store.tabs.push(normalTab);
  108. store.affixTabs.push(affixTab);
  109. expect(store.getTabs).toContainEqual(normalTab);
  110. expect(store.affixTabs).toContainEqual(affixTab);
  111. });
  112. it('navigates to a specific tab', async () => {
  113. const store = useTabbarStore();
  114. const tab: any = { meta: {}, name: 'Dashboard', path: '/dashboard' };
  115. await store._goToTab(tab, router);
  116. expect(router.replace).toHaveBeenCalledWith({
  117. params: {},
  118. path: '/dashboard',
  119. query: {},
  120. });
  121. });
  122. it('closes multiple tabs by paths', async () => {
  123. const store = useTabbarStore();
  124. store.addTab({
  125. fullPath: '/home',
  126. meta: {},
  127. name: 'Home',
  128. path: '/home',
  129. } as any);
  130. store.addTab({
  131. fullPath: '/about',
  132. meta: {},
  133. name: 'About',
  134. path: '/about',
  135. } as any);
  136. store.addTab({
  137. fullPath: '/contact',
  138. meta: {},
  139. name: 'Contact',
  140. path: '/contact',
  141. } as any);
  142. await store._bulkCloseByPaths(['/home', '/contact']);
  143. expect(store.tabs).toHaveLength(1);
  144. expect(store.tabs[0]?.name).toBe('About');
  145. });
  146. it('closes all tabs to the left of the specified tab', async () => {
  147. const store = useTabbarStore();
  148. store.addTab({
  149. fullPath: '/home',
  150. meta: {},
  151. name: 'Home',
  152. path: '/home',
  153. } as any);
  154. store.addTab({
  155. fullPath: '/about',
  156. meta: {},
  157. name: 'About',
  158. path: '/about',
  159. } as any);
  160. const targetTab: any = {
  161. fullPath: '/contact',
  162. meta: {},
  163. name: 'Contact',
  164. path: '/contact',
  165. };
  166. store.addTab(targetTab);
  167. await store.closeLeftTabs(targetTab);
  168. expect(store.tabs).toHaveLength(1);
  169. expect(store.tabs[0]?.name).toBe('Contact');
  170. });
  171. it('closes all tabs except the specified tab', async () => {
  172. const store = useTabbarStore();
  173. store.addTab({
  174. fullPath: '/home',
  175. meta: {},
  176. name: 'Home',
  177. path: '/home',
  178. } as any);
  179. const targetTab: any = {
  180. fullPath: '/about',
  181. meta: {},
  182. name: 'About',
  183. path: '/about',
  184. };
  185. store.addTab(targetTab);
  186. store.addTab({
  187. fullPath: '/contact',
  188. meta: {},
  189. name: 'Contact',
  190. path: '/contact',
  191. } as any);
  192. await store.closeOtherTabs(targetTab);
  193. expect(store.tabs).toHaveLength(1);
  194. expect(store.tabs[0]?.name).toBe('About');
  195. });
  196. it('closes all tabs to the right of the specified tab', async () => {
  197. const store = useTabbarStore();
  198. const targetTab: any = {
  199. fullPath: '/home',
  200. meta: {},
  201. name: 'Home',
  202. path: '/home',
  203. };
  204. store.addTab(targetTab);
  205. store.addTab({
  206. fullPath: '/about',
  207. meta: {},
  208. name: 'About',
  209. path: '/about',
  210. } as any);
  211. store.addTab({
  212. fullPath: '/contact',
  213. meta: {},
  214. name: 'Contact',
  215. path: '/contact',
  216. } as any);
  217. await store.closeRightTabs(targetTab);
  218. expect(store.tabs).toHaveLength(1);
  219. expect(store.tabs[0]?.name).toBe('Home');
  220. });
  221. it('closes the tab with the specified key', async () => {
  222. const store = useTabbarStore();
  223. const keyToClose = '/about';
  224. store.addTab({
  225. fullPath: '/home',
  226. meta: {},
  227. name: 'Home',
  228. path: '/home',
  229. } as any);
  230. store.addTab({
  231. fullPath: keyToClose,
  232. meta: {},
  233. name: 'About',
  234. path: '/about',
  235. } as any);
  236. store.addTab({
  237. fullPath: '/contact',
  238. meta: {},
  239. name: 'Contact',
  240. path: '/contact',
  241. } as any);
  242. await store.closeTabByKey(keyToClose, router);
  243. expect(store.tabs).toHaveLength(2);
  244. expect(
  245. store.tabs.find((tab) => tab.fullPath === keyToClose),
  246. ).toBeUndefined();
  247. });
  248. it('refreshes the current tab', async () => {
  249. const store = useTabbarStore();
  250. const currentTab: any = {
  251. fullPath: '/dashboard',
  252. meta: { name: 'Dashboard' },
  253. name: 'Dashboard',
  254. path: '/dashboard',
  255. };
  256. router.currentRoute.value = currentTab;
  257. await store.refresh(router);
  258. expect(store.excludeCachedTabs.has('Dashboard')).toBe(false);
  259. expect(store.renderRouteView).toBe(true);
  260. });
  261. });