Skip to content

Commit

Permalink
fix: Fixed method call closes #120 #121
Browse files Browse the repository at this point in the history
  • Loading branch information
avil13 committed Jan 28, 2021
1 parent ee7c23f commit 2cad309
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 37 deletions.
39 changes: 28 additions & 11 deletions packages/vue-sweetalert2/__tests__/swal-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import Swal from 'sweetalert2';

import VueSweetalert2 from '../src';

const factory = () => {
const factory = (option = {}) => {
const localVue = createLocalVue();

localVue.use(VueSweetalert2);
localVue.use(VueSweetalert2, option);

return localVue;
};
Expand All @@ -26,23 +26,40 @@ function getAllMethodsNames(): string[][] {

const allMethodsNames = getAllMethodsNames();

beforeAll(() => {
// jest (or more precisely, jsdom) doesn't implement `window.scrollTo` so we need to mock it
// eslint-disable-next-line @typescript-eslint/no-empty-function
window.scrollTo = () => {}
})

describe('Vue-SweetAlert2 swal methods v.8.x', () => {
it.skip('should fire onOpen option key', () => {
const Vue = factory();
const onOpen = jest.fn();
it.skip('should fire onOpen option key', async () => {
const Vue = factory({ title: 'Test title'});
const didOpenMock = jest.fn();

Vue.swal({
animation: false,
onOpen,
await Vue.swal.fire({
showClass: {
popup: '',
container: ''
},
didOpen: () => {
Vue.swal.clickConfirm();
didOpenMock();
}
});

// TODO: add global window mock
expect(onOpen).toBeCalled();
expect(didOpenMock).toBeCalled();
});

it.each(allMethodsNames)('should check methods', method => {
it.each(allMethodsNames)('should check methods "%s"', method => {
const Vue = factory();

expect(Vue.swal[method]).toBeTruthy();
});

it('isLoading()', () => {
const Vue = factory();

expect(typeof Vue.swal.isLoading).toBe('function');
})
});
2 changes: 1 addition & 1 deletion packages/vue-sweetalert2/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = {
// A set of global variables that need to be available in all test environments
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
tsconfig: 'tsconfig.json',
diagnostics: true,
},
'vue-jest': {
Expand Down
3 changes: 3 additions & 0 deletions packages/vue-sweetalert2/nuxt/plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Vue from 'vue';
import VueSweetalert2 from 'vue-sweetalert2';

import 'sweetalert2/dist/sweetalert2.min.css';

Vue.use(VueSweetalert2, <%= JSON.stringify(options, null, 2) %>);

export default ({}, inject) => {
inject('swal', Vue.swal)
}
36 changes: 20 additions & 16 deletions packages/vue-sweetalert2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"test": "jest --runInBand",
"test:watch": "jest --watch",
"test:coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"prepublish": "npm run test && npm run build && auto-changelog",
"changelog": "auto-changelog git add ./CHANGELOG.md",
"prepublish": "npm run test && npm run build && npm run changelog",
"__postpublish": "npm run test:coveralls",
"lint:prettier": "prettier --write src/*.ts",
"lint:ts": "tsc --noEmit --skipLibCheck",
Expand All @@ -27,6 +28,24 @@
"type": "git",
"url": "git+https://github.com/avil13/vue-sweetalert2.git"
},
"nodemonConfig": {
"watch": ["src"],
"exec": "npm run prepublish",
"delay": 2500
},
"lint-staged": {
"*.{js,html,vue,md,json}": ["npm run lint:prettier"],
"*.{ts}": ["npm run lint:prettier && npm run lint:ts"]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm test && npm run changelog",
"pre-push": "lint-staged && npm test"
}
},
"peerDependencies": {
"vue": "*"
},
"devDependencies": {
"@types/jest": "^26.0.19",
"@types/node": "^14.14.16",
Expand Down Expand Up @@ -55,20 +74,5 @@
"vue": "^2.6.12",
"vue-jest": "^3.0.7",
"vue-template-compiler": "^2.6.12"
},
"nodemonConfig": {
"watch": ["src"],
"exec": "npm run prepublish",
"delay": 2500
},
"lint-staged": {
"*.{js,html,vue,md,json}": ["npm run lint:prettier"],
"*.{ts}": ["npm run lint:prettier && npm run lint:ts"]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm test",
"pre-push": "lint-staged && npm test"
}
}
}
15 changes: 6 additions & 9 deletions packages/vue-sweetalert2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {SweetAlertOptions} from 'sweetalert2';
import Swal from 'sweetalert2/dist/sweetalert2.js';
import Vue from 'vue';

export * from 'sweetalert2';

type TVueSwalInstance = typeof Swal & typeof Swal.fire;

Expand All @@ -17,25 +16,23 @@ declare module 'vue/types/vue' {
}

class VueSweetalert2 {
static install(vue: Vue | any, options?: SweetAlertOptions): void {
const swalLocalInstance = options ? Swal.mixin(options) : Swal;
static install(vue: Vue | any, options: SweetAlertOptions = {}): void {
const swalLocalInstance: typeof Swal = Swal.mixin(options);

const swalFunction = (...args: Parameters<typeof Swal['fire']>) => {
return swalLocalInstance.fire.call(swalLocalInstance, ...args);
};

Object.assign(swalFunction, Swal);

let methodName: string | number | symbol;

for (methodName in swalLocalInstance) {
for (methodName in Swal) {
if (
Object.prototype.hasOwnProperty.call(swalLocalInstance, methodName) &&
typeof swalLocalInstance[methodName] === 'function'
) {
swalFunction[methodName] = (method => {
return (...args: Parameters<typeof Swal['fire']>) => {
return swalLocalInstance[method].call(swalLocalInstance, ...args);
};
})(methodName);
swalFunction[methodName] = swalLocalInstance[methodName].bind(swalLocalInstance);
}
}

Expand Down

0 comments on commit 2cad309

Please sign in to comment.