Skip to content

chore: Improve test coverage for realtime_client #1191

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
136 changes: 102 additions & 34 deletions packages/realtime_client/test/channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,12 @@ void main() {
channel = socket.channel('topic');
});

test('sets state to joining', () {
channel.subscribe();

expect(channel.isJoining, true);
});

test('sets joinedOnce to true', () {
test('sets state and joinedOnce when subscribing', () {
expect(channel.joinedOnce, isFalse);

channel.subscribe();

expect(channel.isJoining, true);
expect(channel.joinedOnce, isTrue);
});

Expand All @@ -99,52 +94,30 @@ void main() {
});
});

group('onError', () {
group('state transitions', () {
setUp(() {
socket = RealtimeClient('/socket');
channel = socket.channel('topic');
channel.subscribe();
});

test("sets state to 'errored'", () {
test('error and close events change state correctly', () {
// Test error state
expect(channel.isErrored, isFalse);

channel.trigger('phx_error');

expect(channel.isErrored, isTrue);
});
});

group('onClose', () {
setUp(() {
// Reset and test close state
socket = RealtimeClient('/socket');
channel = socket.channel('topic');
channel.subscribe();
});

test("sets state to 'closed'", () {
expect(channel.isClosed, isFalse);

channel.trigger('phx_close');

expect(channel.isClosed, isTrue);
});
});

group('onMessage', () {
setUp(() {
socket = RealtimeClient('/socket');

channel = socket.channel('topic');
});

test('returns payload by default', () {
final payload = channel.onMessage('event', {'one': 'two'});

expect(payload, {'one': 'two'});
});
});

group('on', () {
late RealtimeChannel channel;

Expand Down Expand Up @@ -352,7 +325,7 @@ void main() {
RealtimeChannel('topic', socket, params: RealtimeChannelConfig());
});

test('description', () async {
test('presence callbacks work correctly', () async {
bool syncCalled = false, joinCalled = false, leaveCalled = false;
channel.onPresenceSync((payload) {
syncCalled = true;
Expand Down Expand Up @@ -386,4 +359,99 @@ void main() {
expect(leaveCalled, isTrue);
});
});

group('postgres changes', () {
setUp(() {
socket = RealtimeClient('', timeout: const Duration(milliseconds: 1234));
channel =
RealtimeChannel('topic', socket, params: RealtimeChannelConfig());
});

test('onPostgresChanges registers postgres change listener', () {
var called = false;
PostgresChangePayload? receivedPayload;

channel.onPostgresChanges(
event: PostgresChangeEvent.insert,
schema: 'public',
table: 'users',
callback: (payload) {
called = true;
receivedPayload = payload;
},
);

// Simulate postgres change event
final payload = {
'event': 'INSERT',
'schema': 'public',
'table': 'users',
'eventType': 'INSERT',
'new': {'id': 1, 'name': 'John'},
'old': {},
'commit_timestamp': '2023-01-01T00:00:00Z',
'errors': null,
};

channel.trigger('postgres_changes', payload);

expect(called, isTrue);
expect(receivedPayload?.eventType, PostgresChangeEvent.insert);
expect(receivedPayload?.newRecord['name'], 'John');
});
});

group('broadcast', () {
setUp(() {
socket = RealtimeClient('', timeout: const Duration(milliseconds: 1234));
channel =
RealtimeChannel('topic', socket, params: RealtimeChannelConfig());
});

test('onBroadcast registers broadcast listener', () {
var called = false;
Map<String, dynamic>? receivedPayload;

channel.onBroadcast(
event: 'chat_message',
callback: (payload) {
called = true;
receivedPayload = payload;
},
);

// Simulate broadcast event
channel.trigger('broadcast', {
'event': 'chat_message',
'payload': {'text': 'Hello world'},
});

expect(called, isTrue);
expect(receivedPayload?['payload']['text'], 'Hello world');
});
});

group('helper methods', () {
setUp(() {
socket = RealtimeClient('', timeout: const Duration(milliseconds: 1234));
channel =
RealtimeChannel('topic', socket, params: RealtimeChannelConfig());
});

test('utility methods work correctly', () {
// replyEventName
expect(channel.replyEventName('ref123'), 'chan_reply_ref123');
expect(channel.replyEventName(null), 'chan_reply_null');

// isMember
expect(channel.isMember('topic'), isTrue);
expect(channel.isMember('other:topic'), isFalse);
expect(channel.isMember('*'), isFalse);

// state getters
expect(channel.isClosed, isTrue);
expect(channel.isErrored, isFalse);
expect(channel.isJoined, isFalse);
});
});
}
47 changes: 47 additions & 0 deletions packages/realtime_client/test/constants_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:realtime_client/src/constants.dart';
import 'package:test/test.dart';

void main() {
group('Constants', () {
test('has correct values', () {
expect(Constants.vsn, '1.0.0');
expect(Constants.defaultTimeout.inMilliseconds, 10000);
expect(Constants.defaultHeartbeatIntervalMs, 25000);
expect(Constants.wsCloseNormal, 1000);
expect(Constants.defaultHeaders, isA<Map<String, String>>());
expect(Constants.defaultHeaders.containsKey('X-Client-Info'), isTrue);
});
});

group('ChannelEventsExtended', () {
test('conversion methods work correctly', () {
// fromType with names
expect(ChannelEventsExtended.fromType('close'), ChannelEvents.close);
expect(ChannelEventsExtended.fromType('error'), ChannelEvents.error);

// fromType with eventNames
expect(ChannelEventsExtended.fromType('phx_close'), ChannelEvents.close);
expect(ChannelEventsExtended.fromType('access_token'),
ChannelEvents.accessToken);
expect(ChannelEventsExtended.fromType('postgres_changes'),
ChannelEvents.postgresChanges);

// eventName returns
expect(ChannelEvents.close.eventName(), 'phx_close');
expect(ChannelEvents.accessToken.eventName(), 'access_token');
expect(ChannelEvents.postgresChanges.eventName(), 'postgres_changes');

// Invalid type throws
expect(
() => ChannelEventsExtended.fromType('invalid_type'),
throwsA(isA<String>().having(
(s) => s, 'error', contains('No type invalid_type exists'))));
});
});

group('Transports', () {
test('has correct websocket value', () {
expect(Transports.websocket, 'websocket');
});
});
}
Loading