Skip to content

Commit

Permalink
support mpx.mixin with stage & fix unit env
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki committed Sep 18, 2021
1 parent d08f749 commit 7a4a7b6
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 42 deletions.
3 changes: 0 additions & 3 deletions babel.config.js

This file was deleted.

24 changes: 24 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"presets": [
[
"@babel/env",
{
"modules": false,
"shippedProposals": true
}
]
],
"sourceType": "unambiguous",
"env": {
"test": {
"presets": [
[
"@babel/env",
{
"shippedProposals": true
}
]
]
}
}
}
9 changes: 0 additions & 9 deletions jest.config.js

This file was deleted.

9 changes: 9 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"transform": {
"^.+\\.(js|jsx)?$": "babel-jest"
},
"moduleNameMapper": {
"\\.(css|styl)$": "identity-obj-proxy"
},
"testEnvironment": "jsdom"
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
"docs:build": "vuepress build docs-vuepress"
},
"devDependencies": {
"@babel/core": "^7.8.7",
"@testing-library/jest-dom": "^4.2.4",
"@types/jest": "^27.0.1",
"@vuepress/plugin-back-to-top": "^1.8.2",
"@vuepress/plugin-pwa": "^1.8.0",
"@vuepress/plugin-search": "^1.8.2",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.9.0",
"eslint": "^5.10.0",
"eslint-config-babel": "^8.0.2",
"eslint-config-standard": "^12.0.0",
Expand All @@ -30,7 +31,7 @@
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^24.8.0",
"jest": "^27.2.0",
"lerna": "^3.4.3",
"typescript": "^4.1.3",
"vue": "^2.6.12",
Expand Down
2 changes: 0 additions & 2 deletions packages/api-proxy/__tests__/web/toast.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ describe('test toast', () => {
expect(toast).toHaveAttribute('class', expect.not.stringContaining('show'))
done()
}, 2500)

jest.runAllTimers()
expect(setTimeout).toHaveBeenCalledTimes(3)
})

test('should show image', () => {
Expand Down
24 changes: 24 additions & 0 deletions packages/api-proxy/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"presets": [
[
"@babel/env",
{
"modules": false,
"shippedProposals": true
}
]
],
"sourceType": "unambiguous",
"env": {
"test": {
"presets": [
[
"@babel/env",
{
"shippedProposals": true
}
]
]
}
}
}
9 changes: 9 additions & 0 deletions packages/api-proxy/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"transform": {
"^.+\\.(js|jsx)?$": "babel-jest"
},
"testEnvironment": "jsdom",
"moduleNameMapper": {
"\\.(css|styl)$": "identity-obj-proxy"
}
}
109 changes: 109 additions & 0 deletions packages/core/__tests__/common/injectMixins.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { injectMixins, mergeInjectedMixins, clearInjectMixins } from '../../src/core/injectMixins'

describe('InjectMixins should work right', () => {
beforeEach(() => {
clearInjectMixins()
})

it('Default inject should be inserted before', () => {
injectMixins({
index: 0
}, 'page')
injectMixins({
index: 1
})

const options = {
mixins: [{
index: 2
}]
}
mergeInjectedMixins(options, 'page')
expect(options.mixins.length).toEqual(3)
options.mixins.forEach((mixin, i) => {
expect(mixin.index).toEqual(i)
})

const options2 = {
mixins: [{
index: 0
}]
}
mergeInjectedMixins(options2, 'component')
expect(options2.mixins.length).toEqual(2)
})

it('Inject with plus stage should be inserted after', () => {
injectMixins({
index: 1
}, {
stage: 1,
types: 'page'
})
const options = {
mixins: [{
index: 0
}]
}
mergeInjectedMixins(options, 'page')
expect(options.mixins.length).toEqual(2)
options.mixins.forEach((mixin, i) => {
expect(mixin.index).toEqual(i)
})

const options2 = {
mixins: [{
index: 0
}]
}
mergeInjectedMixins(options2, 'component')
expect(options2.mixins.length).toEqual(1)
})

it('Inject with stage multiply should be inserted with right order', () => {
injectMixins({
index: 5
}, {
stage: 100,
types: ['page']
})
injectMixins({
index: 4
}, {
stage: 50,
types: 'page'
})

injectMixins([
{
index: 1
}, {
index: 2
}
], {
stage: -100,
types: ['page']
})

injectMixins([
{
index: 0
}
], {
stage: -1000,
types: ['page']
})

const options = {
mixins: [{
index: 3
}]
}

mergeInjectedMixins(options, 'page')
expect(options.mixins.length).toEqual(6)
options.mixins.forEach((mixin, i) => {
expect(mixin.index).toEqual(i)
})
})
})
24 changes: 24 additions & 0 deletions packages/core/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"presets": [
[
"@babel/env",
{
"modules": false,
"shippedProposals": true
}
]
],
"sourceType": "unambiguous",
"env": {
"test": {
"presets": [
[
"@babel/env",
{
"shippedProposals": true
}
]
]
}
}
}
6 changes: 6 additions & 0 deletions packages/core/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"transform": {
"^.+\\.(js|jsx)?$": "babel-jest"
},
"testEnvironment": "jsdom"
}
67 changes: 46 additions & 21 deletions packages/core/src/core/injectMixins.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,63 @@
const MIXINS_MAPS = {
app: [],
page: [],
component: []
import flatten from 'lodash/flatten.js'

const mixinsQueueMap = {
app: [[], []],
page: [[], []],
component: [[], []]
}

export function clearInjectMixins () {
mixinsQueueMap.app = [[], []]
mixinsQueueMap.page = [[], []]
mixinsQueueMap.component = [[], []]
}

export function injectMixins (mixins, type) {
if (typeof type === 'string') {
type = [type]
} else {
type = ['app', 'page', 'component']
export function injectMixins (mixins, options = {}) {
if (typeof options === 'string' || Array.isArray(options)) {
options = {
types: options
}
}

let types = options.types || ['app', 'page', 'component']
let stage = options.stage || -1

if (typeof types === 'string') {
types = [types]
}

if (!Array.isArray(mixins)) {
mixins = [mixins]
}
type.forEach(key => {
const curMixins = MIXINS_MAPS[key]
if (curMixins) {
for (const mixin of mixins) {
curMixins.indexOf(mixin) === -1 && curMixins.push(mixin)

mixins.stage = stage

types.forEach(type => {
const mixinsQueue = stage < 0 ? mixinsQueueMap[type][0] : mixinsQueueMap[type][1]
for (let i = 0; i <= mixinsQueue.length; i++) {
if (i === mixinsQueue.length) {
mixinsQueue.push(mixins)
break
}
const item = mixinsQueue[i]
if (mixins === item) break
if (stage < item.stage) {
mixinsQueue.splice(i, 0, mixins)
break
}
}
})
return this
}

export function getInjectedMixins (type) {
return MIXINS_MAPS[type].slice(0)
return this
}

export function mergeInjectedMixins (options, type) {
const injectedMixins = getInjectedMixins(type)
if (injectedMixins.length) {
options.mixins = options.mixins ? injectedMixins.concat(options.mixins) : injectedMixins
const before = flatten(mixinsQueueMap[type][0])
const middle = options.mixins || []
const after = flatten(mixinsQueueMap[type][1])
const mixins = before.concat(middle).concat(after)
if (mixins.length) {
options.mixins = mixins
}
return options
}
12 changes: 7 additions & 5 deletions packages/core/src/helper/MpxScroll/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ export default class MpxScroll {
)
}

pageScrollTo ({
scrollTop,
selector,
duration = 300
}) {
pageScrollTo (
{
scrollTop,
selector,
duration = 300
}
) {
let _scrollTop

if (isDef(scrollTop)) {
Expand Down

0 comments on commit 7a4a7b6

Please sign in to comment.