-
Notifications
You must be signed in to change notification settings - Fork 2
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
COL-685: Write Models integration tests #160
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
37009c3
to
a03fdf2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great first pass, left some comments :)
}, | ||
}); | ||
|
||
await subscriptionCalls[2]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also await subscriptionCalls[0]
on L119 and assert initial state, then subscriptionCalls[1]
on L132 and assert optimistic state, and finally subscriptionCalls[2]
and assert confirmed state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you say "initial state" do you mean model.state
? Because model.data
only has optimistic
and confirmed
doesn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the initial value that the model state should hold after the sync
call, i.e. the initial value of model.data.confirmed
before the published message is processed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're also currently asserting only on the value of model.data.confirmed/optimistic
rather than the value the the subscription callback is actually invoked with, that would be worth asserting on too
}, | ||
}); | ||
|
||
await subscriptionCalls[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should still await subscriptionCalls[0]
and assert initial state, and assert that the confirmation
promise returned by the optimistic
call rejects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have a look, did I do it as you meant it?
src/Model.integration.test.ts
Outdated
|
||
try { | ||
await confirmation; | ||
} catch (e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test doesn't assert that confirmation
rejects, tests will pass if it resolves as the catch block is never run
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use await expect(promise).rejects.toThrow()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should really be the same as the one above, i.e. it still asserts that the subscribe
callback is invoked correctly
src/Model.integration.test.ts
Outdated
eventData, | ||
}) => { | ||
const channel = ably.channels.get(channelName); | ||
const blazinglyFastEvent = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to be a party pooper, but this naming is probably confusing; there's nothing special about this even that makes it fast, so I'd prefer to just call it 'event' or something sensible 😛
|
||
model.subscribe(subscriptionSpy); | ||
|
||
const [confirmation] = await model.optimistic(eventData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we are setting the optimistic data to the same data the model was intialised with from the sync
call. I think it would be more robust to apply different data optimistically in this test (and in the others) and assert on that
src/Model.integration.test.ts
Outdated
context.eventData = { | ||
name: 'update', | ||
data: { | ||
noOfMessages: 34, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a confusing property, as 34 messages are never sent; would be better to just call the key foo
await subscriptionCalls[5]; | ||
await confirmation; | ||
|
||
expect(model.data.confirmed).toEqual({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is doing what we expect:
- The initial confirmed data is
eventData.data
as that is what is passed tosync
- We then apply the same event optimistically (no detectable change in the data that we can assert on)
- We then publish the confirmed update
blazinglyFastEvent
;eventData.data
will be rebased - We then confirm
eventData.data
We should fix 2. (this issue is present in other tests per prev comment)
We should also avoid using the spread operator here to merge in object keys, as this doesn't allow us to tell the order in which updates were applied (since we are using distinct keys so both will be present in the resulting object regardless of the order the updates are applied in). Instead it would be better to use e.g. an array, which allows us to see the order, i.e. that blazinglyFastEvent
comes first and eventData
comes second.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was lazy and didn't change the sync data into an array but I did change the object properties, so it's more visible as to what data came in first and last. Let me know if you don't like it, I'll change that into an array. 🙃
Another thought: it would be good to add a test of the replay from history functionality too: https://ably.com/docs/livesync/models/models#history |
a03fdf2
to
43e4337
Compare
560d766
to
494268a
Compare
18f729b
to
c684db2
Compare
c684db2
to
47372d1
Compare
const finalData = { ...syncData[1].data, ...eventData.data }; | ||
|
||
model.subscribe(subscriptionSpy); | ||
await model.optimistic(eventData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last missing piece: let's capture the confirmation promise here:
const [confirmation] = await model.optimistic(eventData);
And then later, assert it rejects:
await expect(confirmation).rejects.toThrow()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving this as-is and leaving final comment on confirmation promise bug to be addressed under DTP-720
No description provided.