Skip to content

Commit

Permalink
Change coverageProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunquakes committed May 29, 2024
1 parent dd69cf8 commit 947601e
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 8 deletions.
36 changes: 36 additions & 0 deletions __test__/api/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sqlite3, { Database } from 'sqlite3'
import sqlite3Rewire, {
options,
execute,
save,
list,
page,
count,
getOne,
updateById,
remove
} from '../../src/db/sqlite3'

jest.mock('sqlite3')

beforeEach(() => {
jest.clearAllMocks()
})

describe('Test user api', () => {
test('Test pageList', async () => {
const getDb = sqlite3Rewire.__get__('getDb')
await getDb()
expect(options.db).toBeInstanceOf(Database)

sqlite3.Database = jest.fn().mockImplementation((filePath, callback) => {
if (callback !== undefined) {
callback(new Error('Test'))
}
})

options.db = undefined
await getDb()
expect(options.db).toBeInstanceOf(sqlite3.Database)
})
})
29 changes: 27 additions & 2 deletions __test__/components/Login.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sqlite3 from 'sqlite3'
import { mount } from '@vue/test-utils'
import Login from '../../src/components/Login.vue'
import Antd from 'ant-design-vue'
import store from '../../src/store'
import i18n from '../../src/i18n'

jest.mock('sqlite3')

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
Expand All @@ -18,12 +21,34 @@ Object.defineProperty(window, 'matchMedia', {
}))
})

test('Test Login.vue', async () => {
describe('Test Login.vue', () => {
const wrapper = mount(Login, {
propsData: {},
global: {
plugins: [Antd, store, i18n]
}
})
expect(wrapper.exists()).toBe(true)

test('Mount component', async () => {
expect(wrapper.exists()).toBe(true)
})

test('Submit the form', async () => {
// Mock database instance
sqlite3.Database = jest.fn().mockImplementation(() => {
return {
get: jest.fn(function (sql, callback) {
callback(undefined, { id: 1, content: 'Hello World!', count: 1 })
})
}
})

// Input username and password.
const usernameInput = wrapper.find('input[id="normal_login_username"]')
const passwordInput = wrapper.find('input[id="normal_login_password"]')
await usernameInput.setValue('admin')
await passwordInput.setValue('admin')

await wrapper.find('form').trigger('submit')
})
})
7 changes: 7 additions & 0 deletions __test__/utils/date.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { datetime } from '../../src/utils/date'
describe('Test date utils', () => {
test('Test datetime', async () => {
const date = new Date('2024-05-29T00:00:00Z')
expect(datetime(date)).toBe('2024-05-29 00:00:00')
})
})
8 changes: 6 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
module.exports = {
transform: {
"\\.[jt]sx?$": "babel-jest",
'\\.[jt]sx?$': 'babel-jest',
'^[^.]+.vue$': '@vue/vue3-jest'
},
testPathIgnorePatterns: ['<rootDir>/node_modules/'],
collectCoverage: true,
collectCoverageFrom: ['src/**/**.vue', 'src/**/**.js', 'src/**/**.ts'],
coverageDirectory: 'coverage',
coverageProvider: 'v8',
coverageReporters: ['text', 'lcov'],
moduleFileExtensions: ['vue', 'js', 'ts'],
testEnvironment: 'jsdom',
testEnvironmentOptions: {
customExportConditions: ['node', 'node-addons']
}
},
// moduleNameMapper: {
// '\\.(css|less)$': '<rootDir>/__test__/styleMock.ts'
// }
}
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sonar.organization=sunquakes
sonar.projectKey=sunquakes_electron-kits

sonar.exclusions=src/lang/**.js
sonar.exclusions=src/lang/**.ts
sonar.javascript.lcov.reportPaths=coverage/lcov.info
2 changes: 1 addition & 1 deletion src/components/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import type { TableColumnsType, FormInstance, Pagination } from 'ant-design-vue'
import type { TableColumnsType, FormInstance } from 'ant-design-vue'
import { pageList, del } from '../api/user'
import { useI18n } from 'vue-i18n'
import Form from './user/Form.vue'
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<script lang="ts" setup>
import { EllipsisOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue'
import type { SelectProps } from 'ant-design-vue'
import { ref, h, reactive } from 'vue'
import { ref, h } from 'vue'
import { useI18n } from 'vue-i18n'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
Expand Down
1 change: 0 additions & 1 deletion src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { h } from 'vue'
import { createMemoryHistory, createRouter } from 'vue-router'

import Layout from '../components/Layout.vue'
Expand Down

0 comments on commit 947601e

Please sign in to comment.