From 9f8b053116ee64a672a7e725c4510bcb261ac931 Mon Sep 17 00:00:00 2001 From: SiyaLab Date: Tue, 27 Jun 2023 09:05:50 +0200 Subject: [PATCH 1/3] updated dependencies, started test work --- pubspec.yaml | 6 +- test/sails_io_test.dart | 247 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 234 insertions(+), 19 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 2af466d..fa93885 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,13 +1,13 @@ name: sails_io -version: 0.0.2 +version: 0.0.3 homepage: https://github.com/sailscastshq/sails_io description: Dart/Flutter Websocket Client SDK for communication with Sails from a mobile application. environment: sdk: ">=2.13.0 <3.0.0" dependencies: - socket_io_client: ^1.0.1 + socket_io_client: ^2.0.2 dev_dependencies: pedantic: ^1.10.0 - test: ^1.16.0 + test: ^1.24.3 diff --git a/test/sails_io_test.dart b/test/sails_io_test.dart index 3b70e01..afb0052 100644 --- a/test/sails_io_test.dart +++ b/test/sails_io_test.dart @@ -1,16 +1,231 @@ -// import 'package:sails_io/sails_io.dart'; -// import 'package:test/test.dart'; - -// void main() { -// group('A group of tests', () { -// final awesome = Awesome(); - -// setUp(() { -// // Additional setup goes here. -// }); - -// test('First Test', () { -// expect(awesome.isAwesome, isTrue); -// }); -// }); -// } +import 'package:socket_io_client/socket_io_client.dart' as socket_io; +import 'package:sails_io/sails_io.dart'; +import 'package:socket_io_common/src/util/event_emitter.dart'; +import 'package:socket_io_client/src/manager.dart'; +import 'package:test/test.dart'; + +// Work in progress...SNM + +void main() { + // Create an instance of SailsIOClient + late SailsIOClient sailsIOClient; + + // Set up the test case + setUp(() { + // Create a mock SocketIOClient + final mockSocket = MockSocketIOClient(); + + // Initialize SailsIOClient with the mock socket + sailsIOClient = SailsIOClient(mockSocket); + }); + + // Define your test cases + test('GET request should be simulated', () { + // Create a mock callback function + void mockCallback(dynamic body, JWR jwr) { + // Assert the response body or any other expectations + expect(body, isNotNull); + expect(jwr.statusCode, equals(200)); + } + + // Simulate a GET request using SailsIOClient + sailsIOClient.get( + url: 'https://example.com', + headers: {'Content-Type': 'application/json'}, + cb: mockCallback, + ); + }); + + // Add more test cases for other methods if needed +} + +// Define a mock class for SocketIOClient +class MockSocketIOClient implements socket_io.Socket { + // late final String id; + final Map> eventListeners = {}; + + @override + Future onconnect(id) async { + // Implement the connect method behavior for the mock client + // For example, you can set the `id` property to a unique identifier + id = 'mock-socket-id'; + } + + @override + Future ondisconnect() async { + // Implement the disconnect method behavior for the mock client + // For example, you can clear all registered event listeners + eventListeners.clear(); + } + + @override + void on(String event, Function callback) { + // Implement the on method behavior for the mock client + // Add the callback function to the list of event listeners for the specified event + if (eventListeners.containsKey(event)) { + eventListeners[event]!.add(callback); + } else { + eventListeners[event] = [callback]; + } + } + + @override + void emit(String event, [dynamic data]) { + // Implement the emit method behavior for the mock client + // Invoke all the event listeners registered for the specified event + if (eventListeners.containsKey(event)) { + for (final callback in eventListeners[event]!) { + callback(data); + } + } + } + + @override + Map acks = {}; + + @override + var auth; + + @override + bool connected = false; + + @override + bool disconnected = true; + + @override + Map flags = {}; + + @override + num ids = 0; + + @override + late Manager io; + + @override + late socket_io.Socket json; + + @override + String nsp = ''; + + @override + Map? opts; + + @override + String? query; + + @override + List receiveBuffer = []; + + @override + List sendBuffer = []; + + @override + List? subs; + + @override + Function ack(id) { + throw UnimplementedError(); + } + + @override + bool get active => false; + + @override + void clearListeners() { + eventListeners.clear(); + } + + @override + socket_io.Socket close() { + throw UnimplementedError(); + } + + @override + socket_io.Socket compress(compress) { + throw UnimplementedError(); + } + + @override + void destroy() {} + + @override + void dispose() {} + + @override + void emitBuffered() {} + + @override + void emitWithAck(String event, data, {Function? ack, bool binary = false}) {} + + @override + bool hasListeners(String event) { + throw UnimplementedError(); + } + + @override + void off(String event, [EventHandler? handler]) {} + + @override + void offAny([AnyEventHandler? handler]) {} + + @override + void onAny(AnyEventHandler handler) {} + + @override + void onack(Map packet) {} + + @override + void once(String event, EventHandler handler) {} + + @override + void onclose(reason) {} + + // @override + // void onconnect(id) {} + + // @override + // void ondisconnect() {} + + @override + void onerror(err) {} + + @override + void onevent(Map packet) {} + + @override + void onopen([_]) {} + + @override + void onpacket(packet) {} + + @override + socket_io.Socket open() { + throw UnimplementedError(); + } + + @override + void packet(Map packet) {} + + @override + socket_io.Socket send(List args) { + throw UnimplementedError(); + } + + @override + void subEvents() {} + + @override + String? id; + + @override + socket_io.Socket connect() { + // TODO: implement connect + throw UnimplementedError(); + } + + @override + socket_io.Socket disconnect() { + // TODO: implement disconnect + throw UnimplementedError(); + } +} From 82440016c2154bbe1e14ff65e31254378322c50c Mon Sep 17 00:00:00 2001 From: SiyaLab Date: Tue, 27 Jun 2023 09:11:19 +0200 Subject: [PATCH 2/3] update --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e227c57..0a9c00e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,4 +3,9 @@ - Initial version. ## 0.0.2 + - Made `data`, `headers` and `cb` optional for virtual requests + +## 0.0.3 + +- Updated to socket_io_client: ^2.0.2 From c297ac71b705484dda0b8566a6dcc48cc5b1ce01 Mon Sep 17 00:00:00 2001 From: SiyaLab Date: Tue, 27 Jun 2023 12:20:27 +0200 Subject: [PATCH 3/3] completed test --- test/sails_io_test.dart | 217 ++++------------------------------------ 1 file changed, 22 insertions(+), 195 deletions(-) diff --git a/test/sails_io_test.dart b/test/sails_io_test.dart index afb0052..99ad1e6 100644 --- a/test/sails_io_test.dart +++ b/test/sails_io_test.dart @@ -1,8 +1,7 @@ -import 'package:socket_io_client/socket_io_client.dart' as socket_io; +import 'package:socket_io_client/socket_io_client.dart' as io; import 'package:sails_io/sails_io.dart'; -import 'package:socket_io_common/src/util/event_emitter.dart'; -import 'package:socket_io_client/src/manager.dart'; import 'package:test/test.dart'; +import 'dart:async'; // Work in progress...SNM @@ -13,10 +12,29 @@ void main() { // Set up the test case setUp(() { // Create a mock SocketIOClient - final mockSocket = MockSocketIOClient(); + final mockSocket = io.io( + 'http://localhost:1337', + io.OptionBuilder().setTransports(['websocket']) + // .disableAutoConnect() + .build()); // Initialize SailsIOClient with the mock socket sailsIOClient = SailsIOClient(mockSocket); + + // socket.connect(); + + sailsIOClient.socket.onConnect((_) { + sailsIOClient.socket.emit('toServer', 'init'); + + var count = 0; + Timer.periodic(const Duration(seconds: 1), (Timer countDownTimer) { + sailsIOClient.socket.emit('toServer', count++); + }); + }); + + sailsIOClient.socket.on('event', (data) => print(data)); + sailsIOClient.socket.on('disconnect', (_) => print('disconnect')); + sailsIOClient.socket.on('fromServer', (_) => print(_)); }); // Define your test cases @@ -38,194 +56,3 @@ void main() { // Add more test cases for other methods if needed } - -// Define a mock class for SocketIOClient -class MockSocketIOClient implements socket_io.Socket { - // late final String id; - final Map> eventListeners = {}; - - @override - Future onconnect(id) async { - // Implement the connect method behavior for the mock client - // For example, you can set the `id` property to a unique identifier - id = 'mock-socket-id'; - } - - @override - Future ondisconnect() async { - // Implement the disconnect method behavior for the mock client - // For example, you can clear all registered event listeners - eventListeners.clear(); - } - - @override - void on(String event, Function callback) { - // Implement the on method behavior for the mock client - // Add the callback function to the list of event listeners for the specified event - if (eventListeners.containsKey(event)) { - eventListeners[event]!.add(callback); - } else { - eventListeners[event] = [callback]; - } - } - - @override - void emit(String event, [dynamic data]) { - // Implement the emit method behavior for the mock client - // Invoke all the event listeners registered for the specified event - if (eventListeners.containsKey(event)) { - for (final callback in eventListeners[event]!) { - callback(data); - } - } - } - - @override - Map acks = {}; - - @override - var auth; - - @override - bool connected = false; - - @override - bool disconnected = true; - - @override - Map flags = {}; - - @override - num ids = 0; - - @override - late Manager io; - - @override - late socket_io.Socket json; - - @override - String nsp = ''; - - @override - Map? opts; - - @override - String? query; - - @override - List receiveBuffer = []; - - @override - List sendBuffer = []; - - @override - List? subs; - - @override - Function ack(id) { - throw UnimplementedError(); - } - - @override - bool get active => false; - - @override - void clearListeners() { - eventListeners.clear(); - } - - @override - socket_io.Socket close() { - throw UnimplementedError(); - } - - @override - socket_io.Socket compress(compress) { - throw UnimplementedError(); - } - - @override - void destroy() {} - - @override - void dispose() {} - - @override - void emitBuffered() {} - - @override - void emitWithAck(String event, data, {Function? ack, bool binary = false}) {} - - @override - bool hasListeners(String event) { - throw UnimplementedError(); - } - - @override - void off(String event, [EventHandler? handler]) {} - - @override - void offAny([AnyEventHandler? handler]) {} - - @override - void onAny(AnyEventHandler handler) {} - - @override - void onack(Map packet) {} - - @override - void once(String event, EventHandler handler) {} - - @override - void onclose(reason) {} - - // @override - // void onconnect(id) {} - - // @override - // void ondisconnect() {} - - @override - void onerror(err) {} - - @override - void onevent(Map packet) {} - - @override - void onopen([_]) {} - - @override - void onpacket(packet) {} - - @override - socket_io.Socket open() { - throw UnimplementedError(); - } - - @override - void packet(Map packet) {} - - @override - socket_io.Socket send(List args) { - throw UnimplementedError(); - } - - @override - void subEvents() {} - - @override - String? id; - - @override - socket_io.Socket connect() { - // TODO: implement connect - throw UnimplementedError(); - } - - @override - socket_io.Socket disconnect() { - // TODO: implement disconnect - throw UnimplementedError(); - } -}