Skip to content

Commit

Permalink
slight improvement to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Oct 6, 2019
1 parent 48cebea commit 63acece
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 108 deletions.
47 changes: 40 additions & 7 deletions test/__snapshots__/action-async.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

exports[`it should support logging 1`] = `
Array [
Object {
"arguments": Array [
1,
],
"name": "f - runid 7 - step 0",
"spyReportStart": true,
"type": "action",
},
Object {
"arguments": Array [
2,
],
"name": "myaction - runid 7 - step 0",
"name": "innerF - runid 8 - step 0",
"spyReportStart": true,
"type": "action",
},
Expand All @@ -24,18 +32,21 @@ Array [
Object {
"spyReportEnd": true,
},
Object {
"spyReportEnd": true,
},
Object {
"arguments": Array [
2,
],
"name": "myaction - runid 7 - step 1",
"name": "innerF - runid 8 - step 1",
"spyReportStart": true,
"type": "action",
},
Object {
"key": "a",
"name": "ObservableObject@16",
"newValue": 5,
"newValue": 3,
"oldValue": 2,
"spyReportStart": true,
"type": "update",
Expand All @@ -47,7 +58,7 @@ Array [
"key": "a",
"name": "ObservableObject@16",
"newValue": 4,
"oldValue": 5,
"oldValue": 3,
"spyReportStart": true,
"type": "update",
},
Expand All @@ -59,16 +70,16 @@ Array [
},
Object {
"arguments": Array [
2,
1,
],
"name": "myaction - runid 7 - step 2",
"name": "f - runid 7 - step 1",
"spyReportStart": true,
"type": "action",
},
Object {
"key": "a",
"name": "ObservableObject@16",
"newValue": 3,
"newValue": 5,
"oldValue": 4,
"spyReportStart": true,
"type": "update",
Expand All @@ -79,5 +90,27 @@ Array [
Object {
"spyReportEnd": true,
},
Object {
"arguments": Array [
1,
],
"name": "f - runid 7 - step 2",
"spyReportStart": true,
"type": "action",
},
Object {
"key": "a",
"name": "ObservableObject@16",
"newValue": 3,
"oldValue": 5,
"spyReportStart": true,
"type": "update",
},
Object {
"spyReportEnd": true,
},
Object {
"spyReportEnd": true,
},
]
`;
170 changes: 69 additions & 101 deletions test/action-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function delayThrow<T>(time: number, value: T) {
})
}

test("it should support async actions", done => {
test("it should support async actions", async () => {
mobx.configure({ enforceActions: "observed" })
const values = []
const x = mobx.observable({ a: 1 })
Expand All @@ -31,16 +31,12 @@ test("it should support async actions", done => {
return x.a
})

setTimeout(() => {
f(2).then(v => {
expect(v).toBe(4)
expect(values).toEqual([1, 2, 3, 4])
done()
})
}, 10)
const v = await f(2)
expect(v).toBe(4)
expect(values).toEqual([1, 2, 3, 4])
})

test("it should support try catch in async", done => {
test("it should support try catch in async", async () => {
mobx.configure({ enforceActions: "observed" })
const values = []
const x = mobx.observable({ a: 1 })
Expand All @@ -58,45 +54,35 @@ test("it should support try catch in async", done => {
return x.a
})

setTimeout(() => {
f(2).then(v => {
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
done()
})
}, 10)
const v = await f(2)
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
})

test("it should support throw from async actions", done => {
actionAsync(async function() {
await task(delay(10, 7))
throw 7
})().then(
() => {
done.fail("should fail")
},
e => {
expect(e).toBe(7)
done()
}
)
test("it should support throw from async actions", async () => {
try {
await actionAsync(async () => {
await task(delay(10, 7))
throw 7
})()
fail("should fail")
} catch (e) {
expect(e).toBe(7)
}
})

test("it should support throw from awaited promise", done => {
actionAsync(async function() {
return await task(delayThrow(10, 7))
})().then(
() => {
done.fail("should fail")
},
e => {
expect(e).toBe(7)
done()
}
)
test("it should support throw from awaited promise", async () => {
try {
await actionAsync(async () => {
return await task(delayThrow(10, 7))
})()
fail("should fail")
} catch (e) {
expect(e).toBe(7)
}
})

test("it should support async action in classes", done => {
test("it should support async action in classes", async () => {
const values = []

mobx.configure({ enforceActions: "observed" })
Expand All @@ -123,17 +109,13 @@ test("it should support async action in classes", done => {
const x = new X()
mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true })

setTimeout(() => {
x.f(2).then(v => {
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
expect(x.a).toBe(5)
done()
})
}, 10)
const v = await x.f(2)
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
expect(x.a).toBe(5)
})

test("it should support async action in classes with a method decorator", done => {
test("it should support async action in classes with a method decorator", async () => {
const values = []

mobx.configure({ enforceActions: "observed" })
Expand All @@ -158,17 +140,13 @@ test("it should support async action in classes with a method decorator", done =
const x = new X()
mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true })

setTimeout(() => {
x.f(2).then(v => {
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
expect(x.a).toBe(5)
done()
})
}, 10)
const v = await x.f(2)
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
expect(x.a).toBe(5)
})

test("it should support async action in classes with a field decorator", done => {
test("it should support async action in classes with a field decorator", async () => {
const values = []

mobx.configure({ enforceActions: "observed" })
Expand All @@ -193,37 +171,36 @@ test("it should support async action in classes with a field decorator", done =>
const x = new X()
mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true })

setTimeout(() => {
x.f(2).then(v => {
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
expect(x.a).toBe(5)
done()
})
}, 10)
const v = await x.f(2)
expect(v).toBe(5)
expect(values).toEqual([1, 2, 5])
expect(x.a).toBe(5)
})

test("it should support logging", done => {
test("it should support logging", async () => {
mobx.configure({ enforceActions: "observed" })
const events = []
const x = mobx.observable({ a: 1 })

const f = actionAsync(async function myaction(initial) {
x.a = initial
x.a = await task(delay(100, 5))
const innerF = actionAsync("innerF", async initial => {
x.a = initial // this runs in action
x.a = await task(delay(100, 3))
x.a = 4
return x.a
})

const f = actionAsync("f", async initial => {
x.a = initial
x.a = await task(innerF(2))
x.a = 5
x.a = await task(delay(100, 3))
return x.a
})
const d = mobx.spy(ev => events.push(ev))

setTimeout(() => {
f(2).then(() => {
expect(stripEvents(events)).toMatchSnapshot()
d()
done()
})
}, 10)
await f(1)
expect(stripEvents(events)).toMatchSnapshot()
d()
})

function stripEvents(events) {
Expand All @@ -235,38 +212,34 @@ function stripEvents(events) {
})
}

test("it should support async actions within async actions", done => {
test("it should support async actions within async actions", async () => {
mobx.configure({ enforceActions: "observed" })
const values = []
const x = mobx.observable({ a: 1 })
mobx.reaction(() => x.a, v => values.push(v), { fireImmediately: true })

const innerF = actionAsync(async function(initial) {
const innerF = actionAsync(async initial => {
x.a = initial // this runs in action
x.a = await task(delay(100, 3))
await task(delay(100, 0))
x.a = 4
return x.a
})

const f1 = actionAsync(async function(initial) {
const f1 = actionAsync(async initial => {
x.a = await task(innerF(initial))
x.a = await task(delay(100, 5))
await task(delay(100, 0))
x.a = 6
return x.a
})

setTimeout(() => {
f1(2).then(v => {
expect(v).toBe(6)
expect(values).toEqual([1, 2, 3, 4, 5, 6])
done()
})
}, 10)
const v = await f1(2)
expect(v).toBe(6)
expect(values).toEqual([1, 2, 3, 4, 5, 6])
})

test("it should support async actions within async actions that throw", done => {
test("it should support async actions within async actions that throw", async () => {
mobx.configure({ enforceActions: "observed" })
const values = []
const x = mobx.observable({ a: 1 })
Expand All @@ -288,17 +261,12 @@ test("it should support async actions within async actions that throw", done =>
return x.a
})

setTimeout(() => {
f(2).then(
() => {
done.fail("should fail")
},
e => {
expect(e).toBe("err")
done()
}
)
}, 10)
try {
await f(2)
fail("should fail")
} catch (e) {
expect(e).toBe("err")
}
})

test("typing", async () => {
Expand Down

0 comments on commit 63acece

Please sign in to comment.