Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复taro-with-weapp的测试用例 #536

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 10 additions & 133 deletions packages/taro-with-weapp/__tests__/lifecycle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createElement, render } from 'nervjs'
import withWeapp from '../src'
import { TaroComponent, delay } from './utils'
import * as sinon from 'sinon'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行需不需要删掉

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit之后自动换行了

import withWeapp from '../src'
import { delay, TaroComponent } from './utils'

describe('lifecycle', () => {
/**
* @type {Element} scratch
Expand Down Expand Up @@ -35,11 +36,11 @@ describe('lifecycle', () => {
expect(spy).toBeCalled()
})

test('onLoad', () => {
test('attached', () => {
const spy = jest.fn()

@withWeapp({
onLoad () {
attached () {
spy()
}
})
Expand All @@ -54,69 +55,6 @@ describe('lifecycle', () => {
expect(spy).toBeCalled()
})

test('onLanuch', (done) => {
const spy = jest.fn()

@withWeapp({
data: {
a: ''
},
onLanuch () {
spy()
this.a = 'a'
}
})
class A extends TaroComponent {
render () {
return <div>{this.a}</div>
}
}

render(<A />, scratch)

expect(spy).toBeCalled()

delay(() => {
expect(scratch.textContent).toBe('a')
done()
})
})

test('onReady', (done) => {
const s1 = sinon.spy()
const s2 = sinon.spy()

@withWeapp({
data: {
a: ''
},
created () {
s1()
this.a = 'a'
},
onReady () {
s2()
}
})
class A extends TaroComponent {
render () {
return <div>{this.a}</div>
}
}

render(<A />, scratch)

expect(s1.called).toBeTruthy()
expect(s2.called).toBeTruthy()

expect(s1.calledBefore(s2))

delay(() => {
expect(scratch.textContent).toBe('a')
done()
})
})

test('ready should work', (done) => {
const s1 = sinon.spy()
const s2 = sinon.spy()
Expand All @@ -129,7 +67,7 @@ describe('lifecycle', () => {
s1()
this.a = 'a'
},
ready () {
attached () {
s2()
}
})
Expand Down Expand Up @@ -176,30 +114,6 @@ describe('lifecycle', () => {
expect(s1.callCount).toBe(1)
})

test('detached', () => {
const s1 = sinon.spy()

@withWeapp({
data: {
a: ''
},
detached () {
s1()
}
})
class A extends TaroComponent {
render () {
return <div>{this.a}</div>
}
}

render(<A />, scratch)

render(<div />, scratch)

expect(s1.callCount).toBe(1)
})

test('onUnload', () => {
const s1 = sinon.spy()

Expand Down Expand Up @@ -233,10 +147,10 @@ describe('lifecycle', () => {
data: {
a: ''
},
onLoad () {
created () {
onLoad()
},
onReady () {
attached () {
onReady()
},
onUnload () {
Expand Down Expand Up @@ -273,7 +187,7 @@ describe('lifecycle', () => {
created () {
created()
},
ready () {
attached () {
ready()
},
detached () {
Expand Down Expand Up @@ -314,44 +228,7 @@ describe('lifecycle', () => {

render(<A />, scratch)

expect(spy).toBeCalledWith({ a: 1 })
expect(spy).toBeCalledWith({})
})

test('onLoad should emit this.$router.params as aruguments', () => {
const spy = jest.fn()

@withWeapp({
onLoad (options) {
spy(options)
}
})
class A extends TaroComponent {
render () {
return <div />
}
}

render(<A />, scratch)

expect(spy).toBeCalledWith({ a: 1 })
})

test('onLanuch should emit this.$router.params as aruguments', () => {
const spy = jest.fn()

@withWeapp({
onLanuch (options) {
spy(options)
}
})
class A extends TaroComponent {
render () {
return <div />
}
}

render(<A />, scratch)

expect(spy).toBeCalledWith({ a: 1 })
})
})
24 changes: 15 additions & 9 deletions packages/taro-with-weapp/__tests__/props.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createElement, render } from 'nervjs'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行

import withWeapp from '../src'
import { TaroComponent, delay } from './utils'
import { delay, TaroComponent } from './utils'

describe('lifecycle', () => {
/**
Expand Down Expand Up @@ -99,7 +100,7 @@ describe('lifecycle', () => {

expect(scratch.textContent).toBe('b')
expect(spy).toBeCalled()
expect(spy).toBeCalledWith('b', 'b')
expect(spy).toBeCalledWith('b', 'a')
})

test('observer should work', () => {
Expand All @@ -126,7 +127,7 @@ describe('lifecycle', () => {

@withWeapp({
data: {
a: 'a'
b: 'b'
}
})
class B extends TaroComponent {
Expand All @@ -136,15 +137,15 @@ describe('lifecycle', () => {
}

render () {
return <A a={this.data.a} />
return <A a={this.data.b} />
}
}

render(<B />, scratch)

expect(scratch.textContent).toBe('a')
expect(scratch.textContent).toBe('b')
expect(spy).toBeCalled()
expect(spy).toBeCalledWith('a', 'a')
expect(spy).toBeCalledWith('b', 'a')

inst.setData({ a: 'b' })
inst.forceUpdate()
Expand All @@ -155,8 +156,8 @@ describe('lifecycle', () => {
const spy = jest.fn()

@withWeapp({
ready () {
this.triggerEvent('fork', 'a', 'b', 'c')
attached () {
this.triggerEvent('fork','a')
}
})
class A extends TaroComponent {
Expand All @@ -178,6 +179,11 @@ describe('lifecycle', () => {

render(<B />, scratch)

expect(spy).toBeCalledWith(...['a', 'b', 'c'].map(s => ({ detail: s })))
expect(spy).toBeCalledWith({
type: 'fork',
detail: 'a',
target: { id: '', dataset: {} },
currentTarget: { id: '', dataset: {} }
})
})
})
15 changes: 8 additions & 7 deletions packages/taro-with-weapp/__tests__/state.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createElement, render } from 'nervjs'

import withWeapp from '../src'
import { TaroComponent, delay } from './utils'
import { delay, TaroComponent } from './utils'

describe('lifecycle', () => {
/**
Expand All @@ -23,7 +24,7 @@ describe('lifecycle', () => {
})
class A extends TaroComponent {
render () {
return <div>{this.a}</div>
return <div>{this.data.a}</div>
}
}

Expand All @@ -40,15 +41,15 @@ describe('lifecycle', () => {
data: {
a: ''
},
ready () {
attached () {
this.setData({
a: 'b'
})
}
})
class A extends TaroComponent {
render () {
return <div>{this.a}</div>
return <div>{this.data.a}</div>
}
}

Expand All @@ -67,15 +68,15 @@ describe('lifecycle', () => {
b: ''
}
},
ready () {
attached () {
this.setData({
'a.b': 'b'
})
}
})
class A extends TaroComponent {
render () {
return <div>{this.a.b}</div>
return <div>{this.data.a.b}</div>
}
}

Expand All @@ -91,7 +92,7 @@ describe('lifecycle', () => {
@withWeapp({
data: {
},
ready () {
attached () {
this.setData({
a: 'b'
})
Expand Down
Loading