Add more tests

This commit is contained in:
Pig Fang 2018-09-08 10:11:44 +08:00
parent 47207476ff
commit 6e4a1dfc6f
9 changed files with 131 additions and 6 deletions

View File

@ -110,7 +110,7 @@
"<rootDir>/resources/assets/tests/setup",
"<rootDir>/resources/assets/tests/utils"
],
"testRegex": "resources/assets/tests/.*\\.(spec|test)\\.js"
"testRegex": "resources/assets/tests/.*\\.(spec|test)\\.js$"
},
"serve": {
"clipboard": false,

View File

@ -1,6 +1,7 @@
import $ from 'jquery';
import { init } from './net';
async function checkForUpdates() {
export async function checkForUpdates() {
const response = await fetch(`${blessing.base_url}/admin/update/check`, init);
if (response.ok) {
@ -13,7 +14,7 @@ async function checkForUpdates() {
}
}
async function checkForPluginUpdates() {
export async function checkForPluginUpdates() {
const response = await fetch(`${blessing.base_url}/admin/plugins/market/check`, init);
if (response.ok) {

View File

@ -1,6 +1,6 @@
import { queryStringify } from './utils';
async function sendFeedback() {
export async function sendFeedback() {
if (document.cookie.replace(/(?:(?:^|.*;\s*)feedback_sent\s*=\s*([^;]*).*$)|^.*$/, '$1')) {
return;
}

View File

@ -1,3 +1,4 @@
import $ from 'jquery';
import { post } from './net';
import { swal } from './notify';
import { trans } from './i18n';

View File

@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`send feedback 1`] = `
Array [
"https://work.prinzeugen.net/statistics/feedback",
Object {
"body": "site_name=Blessing%20Skin&site_url=&version=4.0.0",
"headers": Object {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
},
"method": "POST",
"mode": "cors",
},
]
`;

View File

@ -0,0 +1,59 @@
import {
checkForUpdates,
checkForPluginUpdates
} from '@/js/check-updates';
import { init } from '@/js/net';
test('check for BS updates', async () => {
window.fetch = jest.fn()
.mockResolvedValueOnce({ ok: false })
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: false })
})
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: true, latest: '4.0.0' })
});
document.body.innerHTML = `
<a href="/admin/update"></a>
`;
await checkForUpdates();
expect(window.fetch).toBeCalledWith('/admin/update/check', init);
expect(document.querySelector('a').innerHTML).toBe('');
await checkForUpdates();
expect(document.querySelector('a').innerHTML).toBe('');
await checkForUpdates();
expect(document.querySelector('a').innerHTML).toContain('4.0.0');
});
test('check for plugins updates', async () => {
window.fetch = jest.fn()
.mockResolvedValueOnce({ ok: false })
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: false })
})
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ available: true, plugins: [{}] })
});
document.body.innerHTML = `
<a href="/admin/plugins/market"></a>
`;
await checkForPluginUpdates();
expect(window.fetch).toBeCalledWith('/admin/plugins/market/check', init);
expect(document.querySelector('a').innerHTML).toBe('');
await checkForPluginUpdates();
expect(document.querySelector('a').innerHTML).toBe('');
await checkForPluginUpdates();
expect(document.querySelector('a').innerHTML).toContain('1');
});

View File

@ -0,0 +1,28 @@
import { sendFeedback } from '@/js/feedback';
test('send feedback', async () => {
window.fetch = jest.fn()
.mockResolvedValueOnce({ ok: false })
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ errno: 1 })
})
.mockResolvedValue({
ok: true,
json: () => Promise.resolve({ errno: 0 })
});
await sendFeedback();
expect(document.cookie).toBe('');
expect(fetch.mock.calls[0]).toMatchSnapshot();
await sendFeedback();
expect(document.cookie).toBe('');
await sendFeedback();
expect(document.cookie).toStartWith('feedback_sent=');
window.fetch.mockClear();
await sendFeedback();
expect(window.fetch).not.toBeCalled();
});

View File

@ -0,0 +1,18 @@
import { logout } from '@/js/logout';
import { post } from '@/js/net';
import { swal } from '@/js/notify';
jest.mock('@/js/net');
jest.mock('@/js/notify');
test('log out', async () => {
swal.mockResolvedValueOnce({ dismiss: 1 }).mockResolvedValueOnce({});
post.mockResolvedValue({ msg: '' });
await logout();
expect(post).not.toBeCalled();
await logout();
expect(post).toBeCalledWith('/auth/logout');
jest.runAllTimers();
});

View File

@ -3,10 +3,12 @@ import Vue from 'vue';
window.blessing = {
base_url: '',
site_name: 'Blessing Skin'
site_name: 'Blessing Skin',
version: '4.0.0'
};
console.log = console.warn = console.error = () => {};
const noop = () => undefined;
Object.keys(console).forEach(method => console[method] = noop);
Vue.prototype.$t = key => key;