Skip to content
Open
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
46 changes: 46 additions & 0 deletions spec/observables/dom/webSocket-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,52 @@ describe('webSocket', () => {
subject.unsubscribe();
});

it('should take a serializer', () => {
const subject = webSocket<{ data: string}>({
url: 'ws://mysocket',
serializer: (e: any) => {
return e.data + '!';
}
});

subject.subscribe();

const socket = MockWebSocket.lastSocket;
socket.open();


['ahoy', 'yarr', 'shove off'].forEach(x => {
subject.next({ data: x });
expect(socket.lastMessageSent).to.equal(x + '!');
});

subject.unsubscribe();
});

it('if the serializer fails it should go down the error path', () => {
const subject = webSocket<string>({
url: 'ws://mysocket',
serializer: (e: any) => {
throw new Error('I am a bad error');
}
});

const error = sinon.spy();
subject.subscribe({ next: (x: any) => {
expect(x).to.equal('this should not happen');
}, error });

const socket = MockWebSocket.lastSocket;
socket.open();

subject.next('weee!');
expect(error).to.have.been.calledWithMatch({ message: 'I am a bad error' });
expect(socket.readyState).to.equal(WebSocketState.CLOSING);
expect(socket.closeCode).to.equal(1000);

subject.unsubscribe();
});

it('should accept a closingObserver', () => {
let calls = 0;
const subject = webSocket<string>(<any>{
Expand Down
5 changes: 4 additions & 1 deletion src/internal/observable/dom/WebSocketSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
const { serializer } = this._config;
socket!.send(serializer!(x!));
} catch (e) {
this.destination!.error(e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say the actual bug was here. We clearly have a handle to the wrong thing. 🤔

this.destination!.error({
code: 1000,
});
observer.error(e);
}
}
},
Expand Down