Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packages): add unit tests to core and ui #1477

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"version": "0.0.0",
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"test": "vitest run"
},
"devDependencies": {
"@types/uuid": "^9.0.6",
"typescript": "~5.2.0"
},
"dependencies": {
"uuid": "^9.0.1"
"uuid": "^9.0.1",
"vitest": "^0.34.6"
}
}
16 changes: 16 additions & 0 deletions packages/core/src/utils/__test__/getMessageId.test.ts
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)
})
})
5 changes: 3 additions & 2 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"module": "ESNext",
"esModuleInterop": true,
"declaration": true,
"forceConsistentCasingInFileNames": true,
Expand All @@ -11,6 +12,6 @@
"strict": true,
"outDir": "./dist",
},
"include": ["src/**/*.ts"],
"include": ["src/**/*.ts", "vitest.config.ts"],
"exclude": ["node_modules"]
}
}
11 changes: 11 additions & 0 deletions packages/core/vitest.config.ts
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}'],
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ExampleComponent.spec.ts
import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import EmptyView from '../components/common/EmptyView.vue'
import EmptyView from '../common/EmptyView.vue'

describe('EmptyView', () => {
it('should render correctly', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// useLinks.spec.ts
import { describe, it, expect } from 'vitest'
import { ref } from 'vue'
import useLinks from '../composables/useLinks'
import useLinks from '../useLinks'

describe('useLinks', () => {
it('should initialize linksMap correctly', () => {
Expand Down
50 changes: 50 additions & 0 deletions packages/ui/src/i18n/__test__/i18n.test.ts
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()
})
})
111 changes: 111 additions & 0 deletions packages/ui/src/router/__test__/router.test.ts
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()
})
})
Loading