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

COL-685: Write Models integration tests #160

Merged
merged 2 commits into from
Mar 18, 2024

Conversation

snikidev
Copy link
Contributor

No description provided.

Copy link

vercel bot commented Feb 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
models ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2024 0:14am

Copy link
Collaborator

@mschristensen mschristensen left a 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];
Copy link
Collaborator

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

Copy link
Contributor Author

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?

Copy link
Collaborator

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

Copy link
Collaborator

@mschristensen mschristensen Mar 12, 2024

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];
Copy link
Collaborator

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.

Copy link
Contributor Author

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?


try {
await confirmation;
} catch (e) {
Copy link
Collaborator

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

Copy link
Collaborator

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()

Copy link
Collaborator

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

eventData,
}) => {
const channel = ably.channels.get(channelName);
const blazinglyFastEvent = {
Copy link
Collaborator

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);
Copy link
Collaborator

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

context.eventData = {
name: 'update',
data: {
noOfMessages: 34,
Copy link
Collaborator

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({
Copy link
Collaborator

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:

  1. The initial confirmed data is eventData.data as that is what is passed to sync
  2. We then apply the same event optimistically (no detectable change in the data that we can assert on)
  3. We then publish the confirmed update blazinglyFastEvent; eventData.data will be rebased
  4. 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.

Copy link
Contributor Author

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. 🙃

@mschristensen
Copy link
Collaborator

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

@snikidev snikidev force-pushed the col-685-model-integration-testing branch from c684db2 to 47372d1 Compare March 12, 2024 20:09
@github-actions github-actions bot temporarily deployed to staging/pull/160/typedoc March 12, 2024 20:10 Inactive
const finalData = { ...syncData[1].data, ...eventData.data };

model.subscribe(subscriptionSpy);
await model.optimistic(eventData);
Copy link
Collaborator

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()

Copy link
Collaborator

@mschristensen mschristensen left a 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

@mschristensen mschristensen merged commit 74c06b8 into main Mar 18, 2024
8 checks passed
@mschristensen mschristensen deleted the col-685-model-integration-testing branch March 18, 2024 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants