anycable / anycable-client

AnyCable / Action Cable JavaScript client for web, Node.js & React Native
MIT License
96 stars 15 forks source link

Testing support #8

Closed palkan closed 3 years ago

palkan commented 3 years ago

Add ability to unit-test channels.

For example, by creating a test cable implementation.

Example

Having a channel:

import { Channel } from "@anycable/core";

class ChatChannel extends Channel {
  static identifier = "ChatChannel";

  async speak(message) {
    return this.perform("speak", { message });
  }

  async leave() {
    // some custom logic
    return this.disconnect();
  }
}

We would like to test it like this (using Jest):


import { ChatChannel } from './channel.js';
import { TestCable } from '@anycable/core/testing';

describe('ChatChannel', () => {
  let channel: ChatChannel
  let cable: TestCable

  beforeEach(() => {
    cable = new TestCable();
    channel = new ChatChannel({ id: '2021' });
    cable.subscribe(channel);
  })

  it('speak perform an action', async () => {
    await channel.speak("hello");

    expect(cable.outgoing).toEqual([{message: "hello"}])
  })

  it('disconnects when leave', () => {
    await channel.leave()

    expect(channel.state).toEqual("disconnected");
  })
})
``
WilhelmYakunin commented 3 years ago

Hi,

what means by the ability to unit-test?)

palkan commented 3 years ago

what means by the ability to unit-test?)

See the example above: we want to make it possible to test channel classes in isolation (i.e., without actual WS connection or whatever).

TheSeally commented 3 years ago

@palkan, I have some questions about test implementation.