-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(packages): add unit tests to core and ui
- Loading branch information
Showing
9 changed files
with
201 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { getMessageId } from '../getMessageId' | ||
|
||
describe('getMessageId', () => { | ||
it('should return a string that starts with "message_" followed by a UUIDv4', () => { | ||
const id = getMessageId() | ||
|
||
expect(typeof id).toBe('string') | ||
|
||
expect(id.startsWith('message_')).toBe(true) | ||
|
||
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i | ||
const uuid = id.split('message_')[1] | ||
expect(uuid).toMatch(uuidRegex) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, | ||
environment: 'node', | ||
root: fileURLToPath(new URL('./', import.meta.url)), | ||
include: ['**/*.test.{ts,js}'], | ||
}, | ||
}) |
2 changes: 1 addition & 1 deletion
2
packages/ui/src/__test__/EmptyView.spec.ts → ...src/components/__test__/EmptyView.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/ui/src/__test__/useLinks.test.ts → ...src/composables/__test__/useLinks.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { ElementI18nMap, i18n } from '../index' // Assuming your i18n setup is in 'index.ts' | ||
|
||
describe('i18nSetup', () => { | ||
it('ElementI18nMap should map language codes to Element Plus locales', () => { | ||
expect(ElementI18nMap['zh']).toBeDefined() | ||
expect(ElementI18nMap['en']).toBeDefined() | ||
expect(ElementI18nMap['ja']).toBeDefined() | ||
expect(ElementI18nMap['hu']).toBeDefined() | ||
expect(ElementI18nMap['tr']).toBeDefined() | ||
}) | ||
|
||
it('i18n messages should be correctly populated from modules', () => { | ||
const messages = i18n.global.messages as unknown as Record<string, any> | ||
// Assertions for English | ||
expect(messages['en']).toBeDefined() | ||
expect(messages['en']['connections']).toBeDefined() | ||
expect(messages['en']['connections']['connections']).toBe('Connections') | ||
|
||
// Assertions for Chinese | ||
expect(messages['zh']).toBeDefined() | ||
expect(messages['zh']['connections']).toBeDefined() | ||
expect(messages['zh']['connections']['connections']).toBe('连接') | ||
|
||
// Assertions for Hungarian | ||
expect(messages['hu']).toBeDefined() | ||
expect(messages['hu']['connections']).toBeDefined() | ||
expect(messages['hu']['connections']['connections']).toBe('Kapcsolatok') | ||
|
||
// Assertions for Japanese | ||
expect(messages['ja']).toBeDefined() | ||
expect(messages['ja']['connections']).toBeDefined() | ||
expect(messages['ja']['connections']['connections']).toBe('接続') | ||
|
||
// Assertions for Turkish | ||
expect(messages['tr']).toBeDefined() | ||
expect(messages['tr']['connections']).toBeDefined() | ||
expect(messages['tr']['connections']['connections']).toBe('Bağlantılar') | ||
}) | ||
|
||
it('i18n should have correct initial configuration', () => { | ||
// Similar to above, ensure that the types are being recognized | ||
const locale = i18n.global.locale as unknown as string | ||
const fallbackLocale = i18n.global.fallbackLocale as unknown as string | ||
expect(locale).toBeDefined() | ||
expect(fallbackLocale).toBe('en') | ||
const messages = i18n.global.messages as unknown as Record<string, any> | ||
expect(messages).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import type { RouteRecordRaw, RouteLocationNormalized } from 'vue-router' | ||
import { getRoutes, createRouterGuard } from '../index' | ||
|
||
// Mock components for testing | ||
const mockConnectionsComponent = vi.fn() | ||
const mockConnectionDetailsComponent = vi.fn() | ||
|
||
// Construct a component map for testing based on your routes.json | ||
const componentMap = { | ||
ConnectionsComponent: mockConnectionsComponent, | ||
ConnectionDetailsComponent: mockConnectionDetailsComponent, | ||
} | ||
|
||
describe('Router', () => { | ||
it('getRoutes should resolve routes and children correctly', () => { | ||
const routes: RouteRecordRaw[] = getRoutes(componentMap) | ||
|
||
// Assert base routes are defined | ||
expect(routes).toBeDefined() | ||
expect(routes.length).toBeGreaterThan(0) | ||
|
||
// Assert the Connections route is correctly resolved | ||
const connectionsRoute = routes.find((r) => r.name === 'Connections') | ||
expect(connectionsRoute).toBeDefined() | ||
expect(connectionsRoute!.component).toBe(mockConnectionsComponent) | ||
|
||
// Assert the ConnectionDetail child route is correctly resolved | ||
const connectionDetailRoute = connectionsRoute!.children?.find((r) => r.name === 'ConnectionDetail') | ||
expect(connectionDetailRoute).toBeDefined() | ||
expect(connectionDetailRoute!.component).toBe(mockConnectionDetailsComponent) | ||
expect(connectionDetailRoute!.props).toBe(true) | ||
}) | ||
it('should redirect to a connection detail page if there is a first connection ID', async () => { | ||
// Mock the getFirstConnectionId to return a specific ID | ||
const getFirstConnectionId = vi.fn(() => '123') | ||
const routerGuard = createRouterGuard(getFirstConnectionId) | ||
|
||
// Mock the to and from route objects | ||
const to = { | ||
name: 'Connections', | ||
params: {}, | ||
} as RouteLocationNormalized | ||
|
||
const from = {} as RouteLocationNormalized | ||
|
||
// Mock the next function | ||
const next = vi.fn() | ||
|
||
// Call the router guard | ||
routerGuard(to, from, next) | ||
|
||
// Assert that getFirstConnectionId was called | ||
expect(getFirstConnectionId).toHaveBeenCalledTimes(1) | ||
|
||
// Assert that next was called with the correct argument | ||
expect(next).toHaveBeenCalledWith('/connections/123') | ||
}) | ||
|
||
it('should not redirect if there is no first connection ID', async () => { | ||
// Mock the getFirstConnectionId to return null | ||
const getFirstConnectionId = vi.fn(() => null) | ||
const routerGuard = createRouterGuard(getFirstConnectionId) | ||
|
||
// Mock the to and from route objects | ||
const to = { | ||
name: 'Connections', | ||
params: {}, | ||
} as RouteLocationNormalized | ||
|
||
const from = {} as RouteLocationNormalized | ||
|
||
// Mock the next function | ||
const next = vi.fn() | ||
|
||
// Call the router guard | ||
routerGuard(to, from, next) | ||
|
||
// Assert that getFirstConnectionId was called | ||
expect(getFirstConnectionId).toHaveBeenCalledTimes(1) | ||
|
||
// Assert that next was called without arguments, indicating no redirect | ||
expect(next).toHaveBeenCalledWith() | ||
}) | ||
|
||
it('should proceed as normal if not navigating to "Connections"', async () => { | ||
// Mock the getFirstConnectionId to return an ID, it shouldn't be called in this case | ||
const getFirstConnectionId = vi.fn(() => '123') | ||
const routerGuard = createRouterGuard(getFirstConnectionId) | ||
|
||
// Mock the to and from route objects | ||
const to = { | ||
name: 'OtherPage', | ||
params: {}, | ||
} as RouteLocationNormalized | ||
|
||
const from = {} as RouteLocationNormalized | ||
|
||
// Mock the next function | ||
const next = vi.fn() | ||
|
||
// Call the router guard | ||
routerGuard(to, from, next) | ||
|
||
// Assert that getFirstConnectionId was not called | ||
expect(getFirstConnectionId).not.toHaveBeenCalled() | ||
|
||
// Assert that next was called without arguments, indicating normal flow | ||
expect(next).toHaveBeenCalledWith() | ||
}) | ||
}) |
Oops, something went wrong.