Skip to content

Commit

Permalink
Throw error on unsupported broadcaster (#396)
Browse files Browse the repository at this point in the history
* Throw error on unsupported broadcaster

* update CS

* fix CS

* fix cs

* fix eslint

* Removed return

* fix CS

* Update echo.ts

* Update echo.test.ts

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
SuperDJ and taylorotwell committed Apr 9, 2024
1 parent 786f814 commit b2931dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export default class Echo {
this.connector = new NullConnector(this.options);
} else if (typeof this.options.broadcaster == 'function') {
this.connector = new this.options.broadcaster(this.options);
} else {
throw new Error(
`Broadcaster ${typeof this.options.broadcaster} ${this.options.broadcaster} is not supported.`
);
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/echo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Echo from '../src/echo';

describe('Echo', () => {
test('it will not throw error for supported driver', () => {
expect(() => new Echo({ broadcaster: 'reverb' })).not.toThrowError(
'Broadcaster string reverb is not supported.'
);

expect(() => new Echo({ broadcaster: 'pusher' })).not.toThrowError(
'Broadcaster string pusher is not supported.'
);

expect(() => new Echo({ broadcaster: 'socket.io' })).not.toThrowError(
'Broadcaster string socket.io is not supported.'
);

expect(() => new Echo({ broadcaster: 'null' })).not.toThrowError('Broadcaster string null is not supported.');

// eslint-disable-next-line @typescript-eslint/no-empty-function
expect(() => new Echo({ broadcaster: () => {} })).not.toThrowError('Broadcaster function is not supported.');
});

test('it will throw error for unsupported driver', () => {
expect(() => new Echo({ broadcaster: 'foo' })).toThrowError('Broadcaster string foo is not supported.');
});
});

0 comments on commit b2931dc

Please sign in to comment.