Skip to content

Commit

Permalink
fix: for vue2.7 (#431)
Browse files Browse the repository at this point in the history
* fix: for vue 2.7

* fix: format
  • Loading branch information
mya-ake authored Sep 20, 2022
1 parent ff36bb7 commit 7dc232d
Show file tree
Hide file tree
Showing 13 changed files with 1,656 additions and 1,592 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as vue from 'vue-demi';
import * as vue from 'vue';

declare const useWindowSize: () => {
width: vue.ComputedRef<number>;
Expand Down
2 changes: 1 addition & 1 deletion option-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare function install(
declare const VueWindowSizePlugin: {
install: typeof install;
};
declare module '@vue/runtime-core' {
declare module 'vue/types/vue' {
interface ComponentCustomProperties {
$windowWidth: number;
$windowHeight: number;
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
"prepare": "husky install"
},
"dependencies": {
"vue-demi": "^0.9.0",
"window-resize-subject": "^1.5.0"
},
"devDependencies": {
"@types/jest": "^26.0.7",
"@vue/test-utils": "^2.0.0-beta.5",
"@vue/test-utils": "^1.3.0",
"husky": "^6.0.0",
"jest": "^26.2.1",
"lint-staged": "^11.0.0",
Expand All @@ -41,7 +40,7 @@
"rollup-plugin-typescript2": "^0.30.0",
"ts-jest": "^26.1.4",
"typescript": "^4.0.2",
"vue": "^3.0.0",
"vue": "^2.7.10",
"vue-jest": "^3.0.6",
"vue-template-compiler": "^2.6.11"
},
Expand Down
2 changes: 1 addition & 1 deletion src/composition-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, computed, onUnmounted } from 'vue-demi';
import { reactive, computed, onUnmounted } from 'vue';
import { createInitailSize } from './shared';
import type {
WindowResizeSubject,
Expand Down
2 changes: 1 addition & 1 deletion src/mixin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive } from 'vue-demi';
import { reactive } from 'vue';
import { createInitailSize } from './shared';
import type {
WindowResizeObserver,
Expand Down
2 changes: 1 addition & 1 deletion src/option-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const VueWindowSizePlugin = { install };

/** types */
// vue 3
declare module '@vue/runtime-core' {
declare module 'vue/types/vue' {
export interface ComponentCustomProperties {
$windowWidth: number;
$windowHeight: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/CompositionComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineComponent } from 'vue-demi';
import { defineComponent } from 'vue';
import { useWindowSize } from '~/index';

export default defineComponent({
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/OptionComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineComponent } from 'vue-demi';
import { defineComponent } from 'vue';

export default defineComponent({
template: `
Expand Down
45 changes: 18 additions & 27 deletions tests/integration/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { shallowMount } from '@vue/test-utils';
import { createLocalVue, shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';

import { VueWindowSizePlugin, vueWindowSizeMixin } from '~/option-api';
import { vueWindowSizeMixin, VueWindowSizePlugin } from '~/option-api';
import OptionComponent from '~fixtures/OptionComponent';
import CompositionComponent from '~fixtures/CompositionComponent';
import { resizeWindow } from '~fixtures/shared';

declare module '@vue/runtime-core' {
declare module 'vue/types/vue' {
export interface ComponentCustomProperties {
windowWidth: number;
windowHeight: number;
Expand All @@ -20,6 +20,9 @@ const WINDOW_SIZE = {
HEIGHT: 800,
};

const localVue = createLocalVue();
localVue.use(VueWindowSizePlugin);

beforeEach(() => {
resizeWindow(WINDOW_SIZE.WIDTH, WINDOW_SIZE.HEIGHT);
jest.runAllTimers();
Expand All @@ -28,21 +31,17 @@ beforeEach(() => {
describe('Plugin', () => {
describe('mounted', () => {
it('has property', () => {
const wrapper = shallowMount(OptionComponent, {
global: {
plugins: [VueWindowSizePlugin],
},
const wrapper = shallowMount(OptionComponent as any, {
localVue,
});

expect(wrapper.vm.$windowWidth).toBe(WINDOW_SIZE.WIDTH);
expect(wrapper.vm.$windowHeight).toBe(WINDOW_SIZE.HEIGHT);
});

it('shown values', () => {
const wrapper = shallowMount(OptionComponent, {
global: {
plugins: [VueWindowSizePlugin],
},
const wrapper = shallowMount(OptionComponent as any, {
localVue,
});

expect(wrapper.get('#width').text()).toBe(String(WINDOW_SIZE.WIDTH));
Expand All @@ -52,10 +51,8 @@ describe('Plugin', () => {

describe('resize event', () => {
it('reactivity', async () => {
const wrapper = shallowMount(OptionComponent, {
global: {
plugins: [VueWindowSizePlugin],
},
const wrapper = shallowMount(OptionComponent as any, {
localVue,
});

resizeWindow(400, 300);
Expand All @@ -70,21 +67,17 @@ describe('Plugin', () => {
describe('Mixin', () => {
describe('mounted', () => {
it('has property', () => {
const wrapper = shallowMount(OptionComponent, {
global: {
mixins: [vueWindowSizeMixin()],
},
const wrapper = shallowMount(OptionComponent as any, {
localVue,
});

expect(wrapper.vm.$windowWidth).toBe(WINDOW_SIZE.WIDTH);
expect(wrapper.vm.$windowHeight).toBe(WINDOW_SIZE.HEIGHT);
});

it('shown values', () => {
const wrapper = shallowMount(OptionComponent, {
global: {
mixins: [vueWindowSizeMixin()],
},
const wrapper = shallowMount(OptionComponent as any, {
localVue,
});

expect(wrapper.get('#width').text()).toBe(String(WINDOW_SIZE.WIDTH));
Expand All @@ -94,10 +87,8 @@ describe('Mixin', () => {

describe('resize event', () => {
it('reactivity', async () => {
const wrapper = shallowMount(OptionComponent, {
global: {
plugins: [VueWindowSizePlugin],
},
const wrapper = shallowMount(OptionComponent as any, {
localVue,
});

resizeWindow(400, 300);
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/composition-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const createTestComponent = (
});

const createSubject = (): WindowResizeSubject =>
(({
({
addObserver: mocks.addObserver,
subscribe: mocks.subscribe,
unsubscribe: mocks.unsubscribe,
} as unknown) as WindowResizeSubject);
} as unknown as WindowResizeSubject);

describe('composition api', () => {
beforeEach(() => {
Expand Down Expand Up @@ -73,14 +73,14 @@ describe('composition api', () => {

it('unsubscribe is called when unmounting the component', () => {
const wrapper = shallowMount(TestComponent);
wrapper.unmount();
wrapper.destroy();
expect(mocks.unsubscribe).toBeCalledTimes(1);
});

it('unsubscribe is not called when at least one component is mounted', () => {
const wrapper = shallowMount(TestComponent);
shallowMount(TestComponent);
wrapper.unmount();
wrapper.destroy();
expect(mocks.unsubscribe).not.toBeCalled();
});
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/mixin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const mocks = {
mocks.addObserver.mockReturnValue(mocks);

const createSubject = (): WindowResizeSubject =>
(({
({
addObserver: mocks.addObserver,
subscribe: mocks.subscribe,
} as unknown) as WindowResizeSubject);
} as unknown as WindowResizeSubject);

describe('mixin', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/public-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const mocks = {
};

const createSubject = (): WindowResizeSubject =>
(({
({
setDelay: mocks.setDelay,
subscribe: mocks.subscribe,
unsubscribe: mocks.unsubscribe,
} as unknown) as WindowResizeSubject);
} as unknown as WindowResizeSubject);

describe('public API', () => {
let api: ReturnType<typeof createPublicAPI>;
Expand Down
Loading

0 comments on commit 7dc232d

Please sign in to comment.