Skip to content

Commit

Permalink
test: add tests for messageSetPagination
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Aug 7, 2024
1 parent 6759d2a commit f9431b2
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 1 deletion.
33 changes: 33 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import sinon from 'sinon';
import { mockChannelQueryResponse } from './test-utils/mockChannelQueryResponse';

import { ChannelState, StreamChat } from '../../src';
import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from '../../src/constants';

const expect = chai.expect;

Expand Down Expand Up @@ -1243,3 +1244,35 @@ describe('Channel _initializeState', () => {
expect(Object.keys(channel.state.members)).deep.to.be.equal(['alice']);
});
});

describe('Channel.query', async () => {
it('should not update pagination for queried message set', async () => {
const client = await getClientWithUser();
const channel = client.channel('messaging', uuidv4());
const mockedChannelQueryResponse = {
...mockChannelQueryResponse,
messages: Array.from({ length: DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE }, generateMsg),
};
const mock = sinon.mock(client);
mock.expects('post').returns(Promise.resolve(mockedChannelQueryResponse));
await channel.query();
expect(channel.state.messageSets.length).to.be.equal(1);
expect(channel.state.messageSets[0].pagination).to.eql({ hasNext: true, hasPrev: true });
mock.restore();
});

it('should update pagination for queried message set to prevent more pagination', async () => {
const client = await getClientWithUser();
const channel = client.channel('messaging', uuidv4());
const mockedChannelQueryResponse = {
...mockChannelQueryResponse,
messages: Array.from({ length: DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE - 1 }, generateMsg),
};
const mock = sinon.mock(client);
mock.expects('post').returns(Promise.resolve(mockedChannelQueryResponse));
await channel.query();
expect(channel.state.messageSets.length).to.be.equal(1);
expect(channel.state.messageSets[0].pagination).to.eql({ hasNext: true, hasPrev: false });
mock.restore();
});
});
36 changes: 36 additions & 0 deletions test/unit/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as utils from '../../src/utils';
import { StreamChat } from '../../src/client';
import { ConnectionState } from '../../src/connection_fallback';
import { StableWSConnection } from '../../src/connection';
import { mockChannelQueryResponse } from './test-utils/mockChannelQueryResponse';
import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from '../../src/constants';

const expect = chai.expect;
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -576,3 +578,37 @@ describe('Client WSFallback', () => {
expect(client.wsFallback).to.be.equal(fallback);
});
});

describe('Channel.queryChannels', async () => {
it('should not update pagination for queried message set', async () => {
const client = await getClientWithUser();
const mockedChannelsQueryResponse = Array.from({ length: 10 }, () => ({
...mockChannelQueryResponse,
messages: Array.from({ length: DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE }, generateMsg),
}));
const mock = sinon.mock(client);
mock.expects('post').returns(Promise.resolve(mockedChannelsQueryResponse));
await client.queryChannels();
Object.values(client.activeChannels).forEach((channel) => {
expect(channel.state.messageSets.length).to.be.equal(1);
expect(channel.state.messageSets[0].pagination).to.eql({ hasNext: true, hasPrev: true });
});
mock.restore();
});

it('should update pagination for queried message set to prevent more pagination', async () => {
const client = await getClientWithUser();
const mockedChannelQueryResponse = Array.from({ length: 10 }, () => ({
...mockChannelQueryResponse,
messages: Array.from({ length: DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE - 1 }, generateMsg),
}));
const mock = sinon.mock(client);
mock.expects('post').returns(Promise.resolve(mockedChannelQueryResponse));
await client.queryChannels();
Object.values(client.activeChannels).forEach((channel) => {
expect(channel.state.messageSets.length).to.be.equal(1);
expect(channel.state.messageSets[0].pagination).to.eql({ hasNext: true, hasPrev: false });
});
mock.restore();
});
});
174 changes: 173 additions & 1 deletion test/unit/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chai from 'chai';
import { axiosParamsSerializer, formatMessage, normalizeQuerySort } from '../../src/utils';
import { axiosParamsSerializer, formatMessage, messageSetPagination, normalizeQuerySort } from '../../src/utils';
import sinon from 'sinon';

const expect = chai.expect;
Expand Down Expand Up @@ -139,3 +139,175 @@ describe('reaction groups fallback', () => {
});
});
});

describe('message set pagination indicators', () => {
const returnedMoreThanRequested = { requestedPageSize: 1, returnedPageSize: 2 };
const returnedCountAsRequested = { requestedPageSize: 2, returnedPageSize: 2 };
const returnedLessThanRequested = { requestedPageSize: 2, returnedPageSize: 1 };

describe('hasPrev is enabled', () => {
const currentPagination = { hasNext: false, hasPrev: false };
const expectedPagination = { hasNext: false, hasPrev: true };
[returnedMoreThanRequested, returnedCountAsRequested].forEach((requestedToReturned) => {
describe(
requestedToReturned.returnedPageSize > requestedToReturned.requestedPageSize
? 'returned more than requested'
: 'returned as requested',
() => {
const params = { currentPagination, ...requestedToReturned };

it('missing message pagination options', () => {
expect(messageSetPagination(params)).to.eql(expectedPagination);
});
it('message pagination options with id_lt', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_lt: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with id_lte', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_lte: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with created_at_before', () => {
expect(
messageSetPagination({ messagePaginationOptions: { created_at_before: 'X' }, ...params }),
).to.eql(expectedPagination);
});
it('message pagination options with created_at_before_or_equal', () => {
expect(
messageSetPagination({
messagePaginationOptions: { created_at_before_or_equal: 'X' },
...params,
}),
).to.eql(expectedPagination);
});
},
);
});
});

describe('hasPrev is disabled', () => {
const currentPagination = { hasNext: false, hasPrev: true };
const expectedPagination = { hasNext: false, hasPrev: false };
describe('returned less than requested', () => {
const params = { currentPagination, ...returnedLessThanRequested };

it('missing message pagination options', () => {
expect(messageSetPagination(params)).to.eql(expectedPagination);
});
it('message pagination options with id_lt', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_lt: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with id_lte', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_lte: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with created_at_before', () => {
expect(
messageSetPagination({ messagePaginationOptions: { created_at_before: 'X' }, ...params }),
).to.eql(expectedPagination);
});
it('message pagination options with created_at_before_or_equal', () => {
expect(
messageSetPagination({
messagePaginationOptions: { created_at_before_or_equal: 'X' },
...params,
}),
).to.eql(expectedPagination);
});
});
});
describe('hasNext is enabled', () => {
const currentPagination = { hasNext: false, hasPrev: false };
const expectedPagination = { hasNext: true, hasPrev: false };

[returnedMoreThanRequested, returnedCountAsRequested].forEach((requestedToReturned) => {
describe(
requestedToReturned.returnedPageSize > requestedToReturned.requestedPageSize
? 'returned more than requested'
: 'returned as requested',
() => {
const params = { currentPagination, ...requestedToReturned };

it('message pagination options with id_gt', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_gt: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with id_gte', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_gte: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with created_at_after', () => {
expect(
messageSetPagination({ messagePaginationOptions: { created_at_after: 'X' }, ...params }),
).to.eql(expectedPagination);
});
it('message pagination options with created_at_after_or_equal', () => {
expect(
messageSetPagination({
messagePaginationOptions: { created_at_after_or_equal: 'X' },
...params,
}),
).to.eql(expectedPagination);
});
},
);
});
});
describe('hasNext is disabled', () => {
const currentPagination = { hasNext: true, hasPrev: true };
const expectedPagination = { hasNext: false, hasPrev: true };
describe('returned less than requested', () => {
const params = { currentPagination, ...returnedLessThanRequested };
it('message pagination options with id_gt', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_gt: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with id_gte', () => {
expect(messageSetPagination({ messagePaginationOptions: { id_gte: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with created_at_after', () => {
expect(messageSetPagination({ messagePaginationOptions: { created_at_after: 'X' }, ...params })).to.eql(
expectedPagination,
);
});
it('message pagination options with created_at_after_or_equal', () => {
expect(
messageSetPagination({
messagePaginationOptions: { created_at_after_or_equal: 'X' },
...params,
}),
).to.eql(expectedPagination);
});
});
});

describe('does not change', () => {
[
['returned more than requested', returnedMoreThanRequested],
['returned as requested', returnedCountAsRequested],
['returned less than requested', returnedLessThanRequested],
].forEach(([scenario, requestedToReturned]) => {
describe(scenario, () => {
const params = { currentPagination: {}, ...requestedToReturned };
it('is provided unrecognized pagination options', () => {
expect(
messageSetPagination({
messagePaginationOptions: { XY: 'X' },
...params,
}),
).to.eql({});
});
});
});
});
});

0 comments on commit f9431b2

Please sign in to comment.