From f71a9b3dd8e1123fd5dd28b20ff691f39289e53e Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 12 Aug 2018 17:56:00 +0800 Subject: [PATCH] Remove old JavaScript files --- .../assets/src/js/__tests__/auth.test.js | 406 ------ .../assets/src/js/__tests__/user.test.js | 1088 ----------------- resources/assets/src/js/auth/captcha.js | 15 - resources/assets/src/js/auth/forgot.js | 51 - resources/assets/src/js/auth/login.js | 69 -- resources/assets/src/js/auth/register.js | 74 -- resources/assets/src/js/auth/reset.js | 53 - 7 files changed, 1756 deletions(-) delete mode 100644 resources/assets/src/js/__tests__/auth.test.js delete mode 100644 resources/assets/src/js/__tests__/user.test.js delete mode 100644 resources/assets/src/js/auth/captcha.js delete mode 100644 resources/assets/src/js/auth/forgot.js delete mode 100644 resources/assets/src/js/auth/login.js delete mode 100644 resources/assets/src/js/auth/register.js delete mode 100644 resources/assets/src/js/auth/reset.js diff --git a/resources/assets/src/js/__tests__/auth.test.js b/resources/assets/src/js/__tests__/auth.test.js deleted file mode 100644 index 43546a07..00000000 --- a/resources/assets/src/js/__tests__/auth.test.js +++ /dev/null @@ -1,406 +0,0 @@ -/* eslint no-unused-vars: "off" */ - -const $ = require('jquery'); -window.$ = window.jQuery = $; - -window.blessing = { - base_url: '/' -}; - -describe('tests for "captcha" module', () => { - it('refresh captcha', async () => { - const url = jest.fn(path => path); - window.url = url; - - document.body.innerHTML = ` - - - `; - - require('../auth/captcha')(); - - expect($('.captcha').attr('src')).toEqual(expect.stringContaining('auth/captcha?')); - expect($('#captcha').val()).toBe(''); - }); -}); - -describe('tests for "login" module', () => { - const modulePath = '../auth/login'; - - it('login', async () => { - const fetch = jest.fn() - .mockImplementationOnce(option => { - option.beforeSend(); - return Promise.resolve({ errno: 0, msg: 'success' }); - }) - .mockImplementationOnce(() => Promise.resolve( - { errno: 1, msg: 'warning1', login_fails: 1 } - )) - .mockImplementationOnce(() => Promise.resolve( - { errno: 1, msg: 'warning2', login_fails: 4 } - )) - .mockImplementationOnce(() => Promise.reject()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const swal = jest.fn(); - const refreshCaptcha = jest.fn(); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.swal = swal; - window.showMsg = jest.fn(); - window.refreshCaptcha = refreshCaptcha; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - -
- - - - `; - - require(modulePath); - - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyIdentification'); - expect($('#identification').is(':focus')).toBe(true); - - $('#identification').val('username'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyPassword'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('password'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyCaptcha'); - expect($('#captcha').is(':focus')).toBe(true); - - $('#captcha').val('captcha'); - await $('button').click(); - expect(fetch).toBeCalledWith(expect.objectContaining({ - type: 'POST', - url: 'auth/login', - dataType: 'json', - data: { - identification: 'username', - password: 'password', - keep: true, - captcha: 'captcha' - } - })); - expect($('button').html()).toBe( - ' auth.loggingIn' - ); - expect($('button').prop('disabled')).toBe(true); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - - $('#captcha-form').css('display', 'none'); - await $('button').click(); - expect($('#captcha-form').css('display')).toBe('none'); - expect(refreshCaptcha).toBeCalled(); - expect(showMsg).toBeCalledWith('warning1', 'warning'); - expect($('button').html()).toBe('auth.login'); - expect($('button').prop('disabled')).toBe(false); - - await $('button').click(); - expect(swal).toBeCalledWith({ type: 'error', html: 'auth.tooManyFails' }); - expect($('#captcha-form').css('display')).not.toBe('none'); - expect(showMsg).toBeCalledWith('warning2', 'warning'); - - await $('button').click(); - expect(showAjaxError).toBeCalled(); - }); -}); - -describe('tests for "register" module', () => { - const modulePath = '../auth/register'; - - it('register', async () => { - const fetch = jest.fn() - .mockImplementationOnce(option => { - option.beforeSend(); - return Promise.resolve({ errno: 0, msg: 'success' }); - }) - .mockImplementationOnce(() => Promise.resolve( - { errno: 1, msg: 'warning' } - )) - .mockImplementationOnce(() => Promise.reject(new Error)); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const swal = jest.fn().mockImplementation(() => Promise.resolve()); - const showMsg = jest.fn(); - const refreshCaptcha = jest.fn(); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.swal = swal; - window.showMsg = showMsg; - window.refreshCaptcha = refreshCaptcha; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - - - -
- - - `; - - require(modulePath); - - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyEmail'); - expect($('#email').is(':focus')).toBe(true); - - $('#email').val('email'); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidEmail'); - expect(showMsg).toBeCalledWith('auth.invalidEmail', 'warning'); - expect($('#email').is(':focus')).toBe(true); - - $('#email').val('a@b.c'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyPassword'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('secret'); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidPassword'); - expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('too_long_password_very_super_long'); - $('#password').blur(); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidPassword'); - expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('password'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyConfirmPwd'); - expect($('#confirm-pwd').is(':focus')).toBe(true); - - $('#confirm-pwd').val('not_same'); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidConfirmPwd'); - expect(showMsg).toBeCalledWith('auth.invalidConfirmPwd', 'warning'); - expect($('#confirm-pwd').is(':focus')).toBe(true); - - $('#confirm-pwd').val('password'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyNickname'); - expect($('#nickname').is(':focus')).toBe(true); - - $('#nickname').val('nickname'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyCaptcha'); - expect($('#captcha').is(':focus')).toBe(true); - - $('#captcha').val('captcha'); - await $('button').click(); - expect(fetch).toBeCalledWith(expect.objectContaining({ - type: 'POST', - url: 'auth/register', - dataType: 'json', - data: { - email: 'a@b.c', - nickname: 'nickname', - password: 'password', - captcha: 'captcha' - } - })); - expect($('button').html()).toBe( - ' auth.registering' - ); - expect($('button').prop('disabled')).toBe(true); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - - await $('button').click(); - expect(refreshCaptcha).toBeCalled(); - expect(showMsg).toBeCalledWith('warning', 'warning'); - expect($('button').html()).toBe('auth.register'); - - await $('button').click(); - expect(showAjaxError).toBeCalled(); - }); -}); - -describe('tests for "forgot" module', () => { - const modulePath = '../auth/forgot'; - - it('forgot password', async () => { - const fetch = jest.fn() - .mockImplementationOnce(option => { - option.beforeSend(); - return Promise.resolve({ errno: 0, msg: 'success' }); - }) - .mockImplementationOnce(() => Promise.resolve( - { errno: 1, msg: 'warning' } - )) - .mockImplementationOnce(() => Promise.reject(new Error)); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const swal = jest.fn(); - const showMsg = jest.fn(); - const refreshCaptcha = jest.fn(); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.swal = swal; - window.showMsg = showMsg; - window.refreshCaptcha = refreshCaptcha; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - -
- - - `; - - require(modulePath); - - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyEmail'); - expect($('#email').is(':focus')).toBe(true); - - $('#email').val('email'); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidEmail'); - expect(showMsg).toBeCalledWith('auth.invalidEmail', 'warning'); - expect($('#email').is(':focus')).toBe(true); - - $('#email').val('a@b.c'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyCaptcha'); - expect($('#captcha').is(':focus')).toBe(true); - - $('#captcha').val('captcha'); - await $('button').click(); - expect(fetch).toBeCalledWith(expect.objectContaining({ - type: 'POST', - url: 'auth/forgot', - dataType: 'json', - data: { - email: 'a@b.c', - captcha: 'captcha' - } - })); - expect($('button').html()).toBe('auth.send'); - expect($('button').prop('disabled')).toBe(true); - expect(showMsg).toBeCalledWith('success', 'success'); - - await $('button').click(); - expect(refreshCaptcha).toBeCalled(); - expect(showMsg).toBeCalledWith('warning', 'warning'); - expect($('button').html()).toBe('auth.send'); - - await $('button').click(); - expect($('button').html()).toBe('auth.send'); - expect($('button').prop('disabled')).toBe(false); - expect(showAjaxError).toBeCalled(); - }); -}); - -describe('tests for "reset" module', () => { - const modulePath = '../auth/reset'; - - it('reset password', async () => { - const fetch = jest.fn() - .mockImplementationOnce(option => { - option.beforeSend(); - return Promise.resolve({ errno: 0, msg: 'success' }); - }) - .mockImplementationOnce(() => Promise.resolve( - { errno: 1, msg: 'warning' } - )) - .mockImplementationOnce(() => Promise.reject(new Error)); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const swal = jest.fn().mockReturnValue(Promise.resolve()); - const showMsg = jest.fn(); - const getQueryString = jest.fn() - .mockReturnValueOnce('expires') - .mockReturnValueOnce('signature'); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.swal = swal; - window.showMsg = showMsg; - window.refreshCaptcha = jest.fn(); - window.getQueryString = getQueryString; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - - - - `; - - require(modulePath); - - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyPassword'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('secret'); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidPassword'); - expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('too_long_password_very_super_long'); - $('#password').blur(); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidPassword'); - expect(showMsg).toBeCalledWith('auth.invalidPassword', 'warning'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('password'); - $('button').click(); - expect(trans).toBeCalledWith('auth.emptyConfirmPwd'); - expect($('#confirm-pwd').is(':focus')).toBe(true); - - $('#confirm-pwd').val('not_same'); - $('button').click(); - expect(trans).toBeCalledWith('auth.invalidConfirmPwd'); - expect(showMsg).toBeCalledWith('auth.invalidConfirmPwd', 'warning'); - expect($('#confirm-pwd').is(':focus')).toBe(true); - - $('#confirm-pwd').val('password'); - await $('button').click(); - expect(getQueryString.mock.calls[0][0]).toBe('expires'); - expect(getQueryString.mock.calls[1][0]).toBe('signature'); - expect(fetch).toBeCalledWith(expect.objectContaining({ - type: 'POST', - url: 'auth/reset/1?expires=expires&signature=signature', - dataType: 'json', - data: { - password: 'password' - } - })); - expect($('button').html()).toBe( - ' auth.resetting' - ); - expect($('button').prop('disabled')).toBe(true); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - - await $('button').click(); - expect(showMsg).toBeCalledWith('warning', 'warning'); - expect($('button').html()).toBe('auth.reset'); - - await $('button').click(); - expect($('button').html()).toBe('auth.reset'); - expect(showAjaxError).toBeCalled(); - }); -}); diff --git a/resources/assets/src/js/__tests__/user.test.js b/resources/assets/src/js/__tests__/user.test.js deleted file mode 100644 index e83d7e6c..00000000 --- a/resources/assets/src/js/__tests__/user.test.js +++ /dev/null @@ -1,1088 +0,0 @@ -/* eslint no-unused-vars: "off" */ - -const $ = require('jquery'); -window.$ = window.jQuery = $; - -describe('tests for "closet" module', () => { - const modulePath = '../user/closet'; - - $.fn.jqPaginator = jest.fn(); - - it('preview textures', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ type: 'skin', hash: 1 })) - .mockReturnValueOnce(Promise.resolve({ type: 'cape', hash: 2 })) - .mockReturnValueOnce(Promise.resolve({ type: 'skin', hash: 3 })) - .mockReturnValueOnce(Promise.reject()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.showAjaxError = showAjaxError; - window.initSkinViewer = jest.fn(); - window.applySkinViewerConfig = jest.fn(); - - document.body.innerHTML = ` -
- - - `; - require(modulePath); - require('../common/skinview3d'); - - await $('#next > .item-body').click(); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'skinlib/info/1', - dataType: 'json' - }); - expect($('#next').hasClass('item-selected')).toBe(true); - expect($.msp.config.skinUrl).toBe('textures/1'); - expect($('#textures-indicator').text()).toBe('general.skin'); - - document.body.innerHTML = ` -
- - -
-
-
- `; - - await $('#next > .item-body').click(); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'skinlib/info/2', - dataType: 'json' - }); - expect($('#next').hasClass('item-selected')).toBe(true); - expect($.msp.config.capeUrl).toBe('textures/2'); - expect($('#textures-indicator').text()).toBe('general.cape'); - - await $('[tid="3"] > .item-body').click(); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'skinlib/info/3', - dataType: 'json' - }); - expect($.msp.config.skinUrl).toBe('textures/3'); - expect($('#textures-indicator').text()).toBe('general.skin & general.cape'); - - await $('#next > .item-body').click(); - expect(showAjaxError).toBeCalled(); - }); - - it('render closet', () => { - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - window.trans = trans; - window.url = url; - window.showAjaxError = jest.fn(); - - document.body.innerHTML = ` - -
-
- `; - const renderCloset = require(modulePath).renderCloset; - - renderCloset([], 'skin'); - expect($('#closet-paginator').css('display')).toBe('none'); - expect(trans).toBeCalledWith('user.emptyClosetMsg', { url: 'skinlib?filter=skin' }); - expect($('#skin-category').html()).toBe( - '
user.emptyClosetMsg
' - ); - - $('input').val('q'); - renderCloset([], 'skin'); - expect($('#skin-category').html()).toBe( - '
general.noResult
' - ); - - renderCloset([{ tid: 1, name: 'name', type: 'steve' }], 'skin'); - expect($('#closet-paginator').css('display')).not.toBe('none'); - expect($('.item').attr('tid')).toBe('1'); - expect($('img').attr('src')).toBe('/preview/1.png'); - expect($('.texture-name').html().trim()).toBe( - 'name (steve)' - ); - expect($('a.more').attr('href')).toBe('/skinlib/show/1'); - expect($('a.more').attr('title')).toBe('user.viewInSkinlib'); - }); - - it('reload closet', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ - items: [], - category: 'skin', - total_pages: 1 - })) - .mockReturnValueOnce(Promise.reject()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` -
-
- -
-
-
- `; - const reloadCloset = require(modulePath).reloadCloset; - - await reloadCloset('skin', 1, 'q'); - expect(fetch).toBeCalledWith({ - type: 'GET', - url: url('user/closet-data'), - dataType: 'json', - data: { - category: 'skin', - page: 1, - perPage: 0, - q: 'q' - } - }); - expect($('#closet-paginator').attr('last-skin-page')).toBe('1'); - - await reloadCloset('skin', 1, 'q'); - expect(showAjaxError).toBeCalled(); - }); - - it('calculate capacity of closet', () => { - document.body.innerHTML = ` -
-
-
- `; - - const getCapacityOfCloset = require(modulePath).getCapacityOfCloset; - expect(getCapacityOfCloset()).toBe(28); - }); - - it('rename item', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const swal = jest.fn() - .mockImplementationOnce(() => Promise.reject()) - .mockImplementationOnce(({ inputValidator }) => { - inputValidator('name'); - return Promise.resolve('name'); - }); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.swal = swal; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` -
-
- -
-
- `; - const renameClosetItem = require(modulePath).renameClosetItem; - - await renameClosetItem(1, 'oldName'); - expect(fetch).not.toBeCalled(); - - await renameClosetItem(1, 'oldName'); - expect(swal).toBeCalledWith(expect.objectContaining({ - title: trans('user.renameClosetItem'), - input: 'text', - inputValue: 'oldName', - showCancelButton: true, - })); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/closet/rename', - dataType: 'json', - data: { tid: 1, new_name: 'name' } - }); - expect(toastr.success).toBeCalledWith('success'); - expect($('span').html('name')); - - await renameClosetItem(1, 'oldName'); - expect(toastr.warning).toBeCalledWith('warning'); - - await renameClosetItem(1, 'oldName'); - expect(showAjaxError).toBeCalled(); - }); - - it('remove item from closet', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const swal = jest.fn() - .mockReturnValueOnce(Promise.reject()) - .mockReturnValueOnce(Promise.resolve()); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.swal = swal; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` -
-
-
- `; - const removeFromCloset = require(modulePath).removeFromCloset; - - await removeFromCloset(1); - expect(fetch).not.toBeCalled(); - - await removeFromCloset(1); - expect(swal).toBeCalledWith({ - text: 'user.removeFromClosetNotice', - type: 'warning', - showCancelButton: true - }); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/closet/remove', - dataType: 'json', - data: { tid: 1 } - }); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - expect(document.getElementById('shouldBeRemoved')).toBeNull(); - expect(trans).toBeCalledWith('user.emptyClosetMsg', { url: url('skinlib?filter=skin') }); - expect($('#skin-category').html()).toBe( - '
user.emptyClosetMsg
' - ); - - await removeFromCloset(1); - expect(toastr.warning).toBeCalledWith('warning'); - - await removeFromCloset(1); - expect(showAjaxError).toBeCalled(); - }); - - it('set avatar', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const swal = jest.fn() - .mockReturnValueOnce(Promise.reject()) - .mockReturnValueOnce(Promise.resolve()); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.swal = swal; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - User Image - `; - const setAsAvatar = require(modulePath).setAsAvatar; - - await setAsAvatar(1); - expect(fetch).not.toBeCalled(); - - await setAsAvatar(1); - expect(swal).toBeCalledWith({ - title: 'user.setAvatar', - text: 'user.setAvatarNotice', - type: 'question', - showCancelButton: true - }); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/profile/avatar', - dataType: 'json', - data: { tid: 1 } - }); - expect(toastr.success).toBeCalledWith('success'); - expect($('img').attr('src').endsWith('src')).toBe(false); - - await setAsAvatar(1); - expect(toastr.warning).toBeCalledWith('warning'); - - await setAsAvatar(1); - expect(showAjaxError).toBeCalled(); - }); - - it('initialize closet', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.reject()) - .mockReturnValueOnce(Promise.resolve({ items: [], category: 'skin', total_pages: 0 })); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const showAjaxError = jest.fn(); - const debounce = jest.fn((func, timer) => func()); - window.fetch = fetch; - window.trans = trans; - window.url = url; - window.showAjaxError = showAjaxError; - window.debounce = debounce; - $.defaultPaginatorConfig = {}; - $.fn.jqPaginator = jest.fn(({ onPageChange }) => onPageChange(0)); - - const { initCloset } = require(modulePath); - await initCloset(); - expect(fetch).not.toBeCalled(); - - document.body.innerHTML = ` -
-
- `; - await initCloset(); - expect(showAjaxError).toBeCalled(); - - await initCloset(); - expect(debounce.mock.calls[0][1]).toBe(350); - expect(fetch).toBeCalledWith({ - type: 'GET', - url: '/user/closet-data', - dataType: 'json' - }); - expect($.fn.jqPaginator).toBeCalled(); - }); - - it('set texture', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const url = jest.fn(path => path); - const toastr = { - success: jest.fn(), - warning: jest.fn(), - info: jest.fn() - }; - const swal = jest.fn(); - const modal = jest.fn(); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.url = url; - window.toastr = toastr; - window.swal = swal; - $.fn.modal = modal; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - -
- `; - const setTexture = require(modulePath).setTexture; - - await setTexture(); - expect(toastr.info).toBeCalledWith('user.emptySelectedPlayer'); - - $('input').prop('checked', true); - await setTexture(); - expect(toastr.info).toBeCalledWith('user.emptySelectedTexture'); - - $('#textures-indicator').data('skin', 1); - $('#textures-indicator').data('cape', 2); - await setTexture(); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/player/set', - dataType: 'json', - data: { 'pid': '1', 'tid[skin]': 1, 'tid[cape]': 2 } - }); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - expect(modal).toBeCalledWith('hide'); - - await setTexture(); - expect(toastr.warning).toBeCalledWith('warning'); - expect(modal.mock.calls.length).toBe(1); - - await setTexture(); - expect(showAjaxError).toBeCalled(); - }); -}); - -describe('tests for "player" module', () => { - const modulePath = '../user/player'; - - it('show player texture preview', async () => { - const url = jest.fn(path => path); - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ - tid_steve: 1, - tid_alex: 2, - tid_cape: 3, - preference: 'default', - player_name: 'name' - })) - .mockReturnValueOnce(Promise.reject()); - const showAjaxError = jest.fn(); - window.url = url; - window.fetch = fetch; - window.showAjaxError = showAjaxError; - window.defaultSteveSkin = 'steve_base64'; - window.initSkinViewer = jest.fn(); - - document.body.innerHTML = ` -
-
-
- `; - require(modulePath); - - await $('#2').click(); - expect($('#1').hasClass('player-selected')).toBe(false); - expect($('#2').hasClass('player-selected')).toBe(true); - expect(fetch).toBeCalledWith({ - type: 'GET', - url: 'user/player/show', - dataType: 'json', - data: { pid: '2' } - }); - - await $('#2').click(); - expect(showAjaxError).toBeCalled(); - - $('#preview-switch').click(); - expect($('#preview-switch').html()).toBe('user.switch3dPreview'); - }); - - it('change player preference', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const url = jest.fn(path => path); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.url = url; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - `; - $('select').on('change', require(modulePath).changePreference); - - await $('select').val('slim').trigger('change'); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/player/preference', - dataType: 'json', - data: { - pid: '1', - preference: 'slim' - } - }); - expect(toastr.warning).not.toBeCalled(); - expect(toastr.success).toBeCalledWith('success'); - - await $('select').trigger('change'); - expect(toastr.warning).toBeCalledWith('warning'); - - await $('select').trigger('change'); - expect(showAjaxError).toBeCalled(); - }); - - it('change player name', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const url = jest.fn(path => path); - const trans = jest.fn(key => key); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const swal = jest.fn() - .mockImplementationOnce(() => Promise.reject()) - .mockImplementationOnce(options => { - options.inputValidator('name'); - return Promise.resolve('name'); - }); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.url = url; - window.trans = trans; - window.toastr = toastr; - window.swal = swal; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - - - - - - - -
1old
- `; - const changePlayerName = require(modulePath).changePlayerName; - - await changePlayerName(1); - expect(fetch).not.toBeCalled(); - - await changePlayerName(1); - expect(swal).toBeCalledWith(expect.objectContaining({ - title: 'user.changePlayerName', - text: 'placeholder', - inputValue: 'old', - input: 'text', - showCancelButton: true - })); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/player/rename', - dataType: 'json', - data: { pid: 1, new_player_name: 'name' } - }); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - expect($('.player-name').html()).toBe('name'); - - await changePlayerName(1); - expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' }); - - await changePlayerName(1); - expect(showAjaxError).toBeCalled(); - }); - - it('show "clear texture" modal dialog', () => { - const { clearTexture } = require(modulePath); - const trans = jest.fn(key => key); - const showModal = jest.fn(); - window.trans = trans; - window.showModal = showModal; - - clearTexture(); - const args = showModal.mock.calls[0]; - expect(args.includes('user.chooseClearTexture')).toBe(true); - expect(args.includes('default')).toBe(true); - }); - - it('submit clearing texture request', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const url = jest.fn(path => path); - const trans = jest.fn(key => key); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - const modal = jest.fn(); - window.fetch = fetch; - window.url = url; - window.trans = trans; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - $.fn.modal = modal; - - document.body.innerHTML = ` - - - - - - `; - const ajaxClearTexture = require(modulePath).ajaxClearTexture; - - ajaxClearTexture(1); - expect(document.getElementById('shouldBeRemoved')).toBeNull(); - expect(document.getElementById('shouldNotBeRemoved')).not.toBeNull(); - expect(toastr.warning).toBeCalledWith('user.noClearChoice'); - - $('#clear-steve').prop('checked', true); - await ajaxClearTexture(1); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/player/texture/clear', - dataType: 'json', - data: { pid: 1, steve: 1, alex: 0, cape: 0 } - }); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - expect(modal).toBeCalledWith('hide'); - - await ajaxClearTexture(1); - expect(swal).lastCalledWith({ type: 'error', html: 'warning' }); - - await ajaxClearTexture(1); - expect(showAjaxError).toBeCalled(); - }); - - it('delete player', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const url = jest.fn(path => path); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const swal = jest.fn() - .mockReturnValueOnce(Promise.reject()) - .mockReturnValueOnce(Promise.resolve()); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.url = url; - window.toastr = toastr; - window.swal = swal; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - `; - const deletePlayer = require(modulePath).deletePlayer; - - await deletePlayer(1); - expect(fetch).not.toBeCalled(); - - await deletePlayer(1); - expect(swal).toBeCalledWith({ - title: 'user.deletePlayer', - text: 'user.deletePlayerNotice', - type: 'warning', - showCancelButton: true, - cancelButtonColor: '#3085d6', - confirmButtonColor: '#d33' - }); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/player/delete', - dataType: 'json', - data: { pid: 1 } - }); - expect(swal).lastCalledWith({ type: 'success', html: 'success' }); - expect(document.getElementById('1')).toBeNull(); - - await deletePlayer(1); - expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' }); - - await deletePlayer(1); - expect(showAjaxError).toBeCalled(); - }); - - it('add a new player', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const url = jest.fn(path => path); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const swal = jest.fn().mockReturnValue(Promise.resolve()); - const modal = jest.fn(); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.url = url; - window.toastr = toastr; - window.swal = swal; - window.showAjaxError = showAjaxError; - $.fn.modal = modal; - - document.body.innerHTML = ` - - `; - const addNewPlayer = require(modulePath).addNewPlayer; - - await addNewPlayer(); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/player/add', - dataType: 'json', - data: { player_name: 'name' } - }); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - expect(modal).toBeCalled(); - - await addNewPlayer(); - expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' }); - - await addNewPlayer(); - expect(showAjaxError).toBeCalled(); - }); -}); - -describe('tests for "profile" module', () => { - const modulePath = '../user/profile'; - - it('change nickname', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const swal = jest.fn() - .mockReturnValueOnce(Promise.resolve()) - .mockReturnValueOnce(Promise.reject()) - .mockReturnValueOnce(Promise.resolve()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.swal = swal; - window.trans = trans; - window.url = url; - window.debounce = jest.fn(fn => fn); - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` -
- - `; - const changeNickName = require(modulePath).changeNickName; - - await changeNickName(); - expect(swal).toBeCalledWith({ type: 'error', html: 'user.emptyNewNickName' }); - expect(fetch).not.toBeCalled(); - - $('input').val('name'); - await changeNickName(); - expect(fetch).not.toBeCalled(); - - await changeNickName(); - expect(trans).toBeCalledWith('user.changeNickName', { new_nickname: 'name' }); - expect(swal).toBeCalledWith({ - text: 'user.changeNickName', - type: 'question', - showCancelButton: true - }); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/profile?action=nickname', - dataType: 'json', - data: { new_nickname: 'name' } - }); - expect($('.nickname').text()).toBe('name'); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - - await changeNickName(); - expect(swal).toBeCalled(); - - await changeNickName(); - expect(showAjaxError).toBeCalled(); - }); - - it('change password', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const swal = jest.fn().mockReturnValue(Promise.resolve()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const toastr = { - info: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.swal = swal; - window.trans = trans; - window.url = url; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - - - `; - const changePassword = require(modulePath).changePassword; - - changePassword(); - expect(toastr.info).toBeCalledWith('user.emptyPassword'); - expect($('#password').is(':focus')).toBe(true); - - $('#password').val('password'); - changePassword(); - expect(toastr.info).toBeCalledWith('user.emptyNewPassword'); - expect($('#new-passwd').is(':focus')).toBe(true); - - $('#new-passwd').val('new-password'); - changePassword(); - expect(toastr.info).toBeCalledWith('auth.emptyConfirmPwd'); - expect($('#confirm-pwd').is(':focus')).toBe(true); - - $('#confirm-pwd').val('not-same').blur(); - changePassword(); - expect(toastr.warning).toBeCalledWith('auth.invalidConfirmPwd'); - expect($('#confirm-pwd').is(':focus')).toBe(true); - - $('#confirm-pwd').val('new-password'); - await changePassword(); - expect(swal).toBeCalledWith({ text: 'success', type: 'success' }); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/profile?action=password', - dataType: 'json', - data: { current_password: 'password', new_password: 'new-password' } - }); - - await changePassword(); - expect(swal).toBeCalledWith({ type: 'warning', text: 'warning' }); - - await changePassword(); - expect(showAjaxError).toBeCalled(); - }); - - it('change email', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.reject()); - const swal = jest.fn() - .mockReturnValueOnce(Promise.resolve()) - .mockReturnValueOnce(Promise.resolve()) - .mockReturnValueOnce(Promise.reject()) - .mockReturnValue(Promise.resolve()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const toastr = { - info: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.swal = swal; - window.trans = trans; - window.url = url; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - - `; - const changeEmail = require(modulePath).changeEmail; - - await changeEmail(); - expect(swal).toBeCalledWith({ type: 'error', html: 'user.emptyNewEmail' }); - expect(fetch).not.toBeCalled(); - - $('#new-email').val('email'); - await changeEmail(); - expect(swal).toBeCalledWith({ type: 'warning', html: 'auth.invalidEmail' }); - - $('#new-email').val('a@b.c'); - await changeEmail(); // Suppose the user cancelled changing email - - await changeEmail(); - expect(trans).toBeCalledWith('user.changeEmail', { new_email: 'a@b.c' }); - expect(swal).toBeCalledWith({ - text: 'user.changeEmail', - type: 'question', - showCancelButton: true - }); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/profile?action=email', - dataType: 'json', - data: { new_email: 'a@b.c', password: 'pwd' } - }); - expect(swal).toBeCalledWith({ type: 'warning', text: 'warning' }); - - await changeEmail(); - expect(swal).toBeCalledWith({ type: 'success', text: 'success' }); - - await changeEmail(); - expect(showAjaxError).toBeCalled(); - }); - - it('delete account', async () => { - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - const swal = jest.fn().mockReturnValue(Promise.resolve()); - const trans = jest.fn(key => key); - const url = jest.fn(path => path); - const toastr = { - info: jest.fn(), - warning: jest.fn() - }; - const showAjaxError = jest.fn(); - window.fetch = fetch; - window.swal = swal; - window.trans = trans; - window.url = url; - window.toastr = toastr; - window.showAjaxError = showAjaxError; - - document.body.innerHTML = ` - - `; - const deleteAccount = require(modulePath).deleteAccount; - - await deleteAccount(); - expect(swal).toBeCalledWith({ type: 'warning', html: 'user.emptyDeletePassword' }); - - $('#password').val('password'); - await deleteAccount(); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/profile?action=delete', - dataType: 'json', - data: { password: 'password' } - }); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - expect(url).toBeCalledWith('auth/login'); - - await deleteAccount(); - expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' }); - - await deleteAccount(); - expect(showAjaxError).toBeCalled(); - }); -}); - -describe('tests for "sign" module', () => { - const modulePath = '../user/sign'; - - it('sign', async () => { - const url = jest.fn(path => path); - const toastr = { - success: jest.fn(), - warning: jest.fn() - }; - const trans = jest.fn(key => key); - const swal = jest.fn().mockReturnValue(Promise.resolve()); - const showAjaxError = jest.fn(); - window.url = url; - window.toastr = toastr; - window.trans = trans; - window.swal = swal; - window.showAjaxError = showAjaxError; - window.debounce = fn => fn; - const fetch = jest.fn() - .mockReturnValueOnce(Promise.resolve({ - errno: 0, - msg: 'success', - score: 100, - remaining_time: 0.1, - storage: { - used: 50, - total: 100, - percentage: 50 - } - })) - .mockReturnValueOnce(Promise.resolve({ - errno: 0, - msg: 'success', - score: 100, - remaining_time: 24, - storage: { - used: 2000, - total: 4000, - percentage: 50 - } - })) - .mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' })) - .mockReturnValueOnce(Promise.reject()); - window.fetch = fetch; - - document.body.innerHTML = ` -
- -
-
- `; - const sign = require(modulePath); - - await sign(); - expect(fetch).toBeCalledWith({ - type: 'POST', - url: 'user/sign', - dataType: 'json' - }); - expect($('#score').html()).toBe('100'); - expect(trans).toBeCalledWith( - 'user.signRemainingTime', - { time: '6', unit: 'user.timeUnitMin' } - ); - expect($('#sign-button').html()).toBe( - '  user.signRemainingTime' - ); - expect($('#sign-button').attr('disabled')).toBe('disabled'); - expect($('#user-storage').html()).toBe('50/ 100 KB'); - expect($('#user-storage-bar').css('width')).toBe('50%'); - expect(swal).toBeCalledWith({ type: 'success', html: 'success' }); - - await sign(); - expect(trans).toBeCalledWith( - 'user.signRemainingTime', - { time: '24', unit: 'user.timeUnitHour' } - ); - expect($('#user-storage').html()).toBe('2/ 4 MB'); - - await sign(); - expect(toastr.warning).toBeCalledWith('warning'); - - await sign(); - expect(showAjaxError).toBeCalled(); - }); -}); diff --git a/resources/assets/src/js/auth/captcha.js b/resources/assets/src/js/auth/captcha.js deleted file mode 100644 index 77a14728..00000000 --- a/resources/assets/src/js/auth/captcha.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -function refreshCaptcha() { - const timestamp = new Date().getTime(); - // Refresh Captcha Image - $('.captcha').attr('src', url(`auth/captcha?${timestamp}`)); - // Clear input - $('#captcha').val(''); -} - -$('.captcha').click(refreshCaptcha); - -if (process.env.NODE_ENV === 'test') { - module.exports = refreshCaptcha; -} diff --git a/resources/assets/src/js/auth/forgot.js b/resources/assets/src/js/auth/forgot.js deleted file mode 100644 index cf391d77..00000000 --- a/resources/assets/src/js/auth/forgot.js +++ /dev/null @@ -1,51 +0,0 @@ -/* global refreshCaptcha */ - -'use strict'; - -$('#forgot-button').click(e => { - e.preventDefault(); - - const data = { - email: $('#email').val(), - captcha: $('#captcha').val() - }; - - (function validate({ email, captcha }, callback) { - if (email === '') { - showMsg(trans('auth.emptyEmail')); - $('#email').focus(); - } else if (!/\S+@\S+\.\S+/.test(email)) { - showMsg(trans('auth.invalidEmail'), 'warning'); - } else if (captcha === '') { - showMsg(trans('auth.emptyCaptcha')); - $('#captcha').focus(); - } else { - callback(); - } - })(data, async () => { - try { - const { errno, msg } = await fetch({ - type: 'POST', - url: url('auth/forgot'), - dataType: 'json', - data: data, - beforeSend: () => { - $('#forgot-button').html( - ' ' + trans('auth.sending') - ).prop('disabled', 'disabled'); - } - }); - if (errno === 0) { - showMsg(msg, 'success'); - $('#forgot-button').html(trans('auth.send')).prop('disabled', 'disabled'); - } else { - showMsg(msg, 'warning'); - refreshCaptcha(); - $('#forgot-button').html(trans('auth.send')).prop('disabled', ''); - } - } catch (error) { - showAjaxError(error); - $('#forgot-button').html(trans('auth.send')).prop('disabled', ''); - } - }); -}); diff --git a/resources/assets/src/js/auth/login.js b/resources/assets/src/js/auth/login.js deleted file mode 100644 index 4219b4cd..00000000 --- a/resources/assets/src/js/auth/login.js +++ /dev/null @@ -1,69 +0,0 @@ -/* global refreshCaptcha */ - -'use strict'; - -$('#login-button').click(async e => { - e.preventDefault(); - - const data = { - identification: $('#identification').val(), - password: $('#password').val(), - keep: $('#keep').prop('checked') ? true : false - }; - - if (data.identification === '') { - showMsg(trans('auth.emptyIdentification')); - $('#identification').focus(); - } else if (data.password === '') { - showMsg(trans('auth.emptyPassword')); - $('#password').focus(); - } else { - // Verify it when captcha form is shown - if ($('#captcha-form').css('display') === 'block') { - data.captcha = $('#captcha').val(); - - if (data.captcha === '') { - showMsg(trans('auth.emptyCaptcha')); - $('#captcha').focus(); - return false; - } - } - - try { - const { errno, msg, login_fails } = await fetch({ - type: 'POST', - url: url('auth/login'), - dataType: 'json', - data: data, - beforeSend: () => { - $('#login-button').html( - ' ' + trans('auth.loggingIn') - ).prop('disabled', 'disabled'); - } - }); - if (errno === 0) { - swal({ type: 'success', html: msg }); - - setTimeout(() => { - window.location = url(blessing.redirect_to || 'user'); - }, 1000); - } else { - if (login_fails > 3) { - if ($('#captcha-form').css('display') === 'none') { - swal({ type: 'error', html: trans('auth.tooManyFails') }); - - $('#captcha-form').show(); - } - } - - refreshCaptcha(); - - showMsg(msg, 'warning'); - $('#login-button').html(trans('auth.login')).prop('disabled', ''); - } - } catch (error) { - showAjaxError(error); - $('#login-button').html(trans('auth.login')).prop('disabled', ''); - } - } -}); diff --git a/resources/assets/src/js/auth/register.js b/resources/assets/src/js/auth/register.js deleted file mode 100644 index 70c028c1..00000000 --- a/resources/assets/src/js/auth/register.js +++ /dev/null @@ -1,74 +0,0 @@ -/* global refreshCaptcha */ - -'use strict'; - -$('#register-button').click(e => { - e.preventDefault(); - - const data = { - email: $('#email').val(), - password: $('#password').val(), - nickname: $('#nickname').val(), - captcha: $('#captcha').val(), - }; - - (function validate({ email, password, nickname, captcha }, callback) { - // Massive form validation - if (email === '') { - showMsg(trans('auth.emptyEmail')); - $('#email').focus(); - } else if (!/\S+@\S+\.\S+/.test(email)) { - showMsg(trans('auth.invalidEmail'), 'warning'); - } else if (password === '') { - showMsg(trans('auth.emptyPassword')); - $('#password').focus(); - } else if (password.length < 8 || password.length > 32) { - showMsg(trans('auth.invalidPassword'), 'warning'); - $('#password').focus(); - } else if ($('#confirm-pwd').val() === '') { - showMsg(trans('auth.emptyConfirmPwd')); - $('#confirm-pwd').focus(); - } else if (password !== $('#confirm-pwd').val()) { - showMsg(trans('auth.invalidConfirmPwd'), 'warning'); - $('#confirm-pwd').focus(); - } else if (nickname === '') { - showMsg(trans('auth.emptyNickname')); - $('#nickname').focus(); - } else if (captcha === '') { - showMsg(trans('auth.emptyCaptcha')); - $('#captcha').focus(); - } else { - callback(); - } - - return; - })(data, async () => { - try { - const { errno, msg } = await fetch({ - type: 'POST', - url: url('auth/register'), - dataType: 'json', - data: data, - beforeSend: function () { - $('#register-button').html( - ' ' + trans('auth.registering') - ).prop('disabled', 'disabled'); - } - }); - if (errno === 0) { - swal({ type: 'success', html: msg }); - - setTimeout(() => { - window.location = url('user'); - }, 1000); - } else { - showMsg(msg, 'warning'); - refreshCaptcha(); - $('#register-button').html(trans('auth.register')).prop('disabled', ''); - } - } catch (error) { - showAjaxError(error); - $('#register-button').html(trans('auth.register')).prop('disabled', ''); - } - }); -}); diff --git a/resources/assets/src/js/auth/reset.js b/resources/assets/src/js/auth/reset.js deleted file mode 100644 index a97d30f2..00000000 --- a/resources/assets/src/js/auth/reset.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict'; - -$('#reset-button').click(e => { - e.preventDefault(); - - const data = { - password: $('#password').val(), - }; - - (function validate({ password }, callback) { - if (password === '') { - showMsg(trans('auth.emptyPassword')); - $('#password').focus(); - } else if (password.length < 8 || password.length > 16) { - showMsg(trans('auth.invalidPassword'), 'warning'); - $('#password').focus(); - } else if ($('#confirm-pwd').val() === '') { - showMsg(trans('auth.emptyConfirmPwd')); - $('#confirm-pwd').focus(); - } else if (password !== $('#confirm-pwd').val()) { - showMsg(trans('auth.invalidConfirmPwd'), 'warning'); - $('#confirm-pwd').focus(); - } else { - callback(); - } - })(data, async () => { - try { - const { errno, msg } = await fetch({ - type: 'POST', - url: `${url('auth/reset')}/${$('#uid').val()}?expires=${getQueryString('expires')}&signature=${getQueryString('signature')}`, - dataType: 'json', - data, - beforeSend: () => { - $('#reset-button').html( - ' ' + trans('auth.resetting') - ).prop('disabled', 'disabled'); - } - }); - if (errno === 0) { - swal({ - type: 'success', - html: msg - }).then(() => (window.location = url('auth/login'))); - } else { - showMsg(msg, 'warning'); - $('#reset-button').html(trans('auth.reset')).prop('disabled', ''); - } - } catch (error) { - showAjaxError(error); - $('#reset-button').html(trans('auth.reset')).prop('disabled', ''); - } - }); -});