Skip to content

Instantly share code, notes, and snippets.

@samuellevy
Created September 24, 2025 18:40
Show Gist options
  • Select an option

  • Save samuellevy/67eb8eb417a2bcee2f792d94ae4357be to your computer and use it in GitHub Desktop.

Select an option

Save samuellevy/67eb8eb417a2bcee2f792d94ae4357be to your computer and use it in GitHub Desktop.
public
import { TestBed } from '@angular/core/testing';
import { NgZone } from '@angular/core';
import { NotifyHubService } from './notify-hub.service';
// Criamos mocks do SignalR
const mockHubConnection = {
start: jest.fn().mockResolvedValue(void 0),
stop: jest.fn().mockResolvedValue(void 0),
invoke: jest.fn().mockResolvedValue(void 0),
on: jest.fn(),
};
const mockHubConnectionBuilder = {
withUrl: jest.fn().mockReturnThis(),
withAutomaticReconnect: jest.fn().mockReturnThis(),
build: jest.fn(() => mockHubConnection),
};
// Mock global do signalR
jest.mock('@microsoft/signalr', () => ({
HubConnectionBuilder: jest.fn(() => mockHubConnectionBuilder),
}));
describe('NotifyHubService', () => {
let service: NotifyHubService;
let zone: NgZone;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [NotifyHubService],
});
service = TestBed.inject(NotifyHubService);
zone = TestBed.inject(NgZone);
});
afterEach(() => {
jest.clearAllMocks();
});
it('deve criar o service', () => {
expect(service).toBeTruthy();
});
it('deve expor um observable de eventos', (done) => {
const evento = { event: 'Teste', payload: { foo: 'bar' } };
service.events$.subscribe((res) => {
expect(res).toEqual(evento);
done();
});
// Forçando emissão manual
(service as any).eventsSubject.next(evento);
});
it('deve iniciar a conexão com startConnection', async () => {
await service.startConnection('/hub', 'fake-token');
expect(mockHubConnectionBuilder.withUrl).toHaveBeenCalled();
expect(mockHubConnection.start).toHaveBeenCalled();
});
it('deve rejeitar se não passar token', async () => {
await expect(service.startConnection('/hub', '')).rejects.toThrow();
});
it('deve chamar stopConnection e encerrar o hub', async () => {
await service.startConnection('/hub', 'fake-token');
await service.stopConnection();
expect(mockHubConnection.stop).toHaveBeenCalled();
});
it('deve registrar listeners e emitir eventos', () => {
const cbMap: Record<string, Function> = {};
mockHubConnection.on.mockImplementation((eventName, cb) => {
cbMap[eventName] = cb;
});
// Conectar
return service.startConnection('/hub', 'fake-token').then(() => {
const spy = jest.fn();
service.events$.subscribe(spy);
// Simula evento vindo do SignalR
cbMap['NotificaSenhaNaoAtendida']({ id: 123 });
expect(spy).toHaveBeenCalledWith({
event: 'NotificaSenhaNaoAtendida',
payload: { id: 123 },
});
});
});
it('deve invocar rejoinGroups corretamente', async () => {
await service.startConnection('/hub', 'fake-token');
(service as any).joinedGroups.secoes.add('secao1');
(service as any).joinedGroups.atendentes.add('atendente1');
(service as any).joinedGroups.supervisor = true;
service.rejoinGroups();
expect(mockHubConnection.invoke).toHaveBeenCalledWith(
'AdicionaAoGrupoSecao',
'secao1',
);
expect(mockHubConnection.invoke).toHaveBeenCalledWith(
'AdicionaAoGrupoAtendente',
'atendente1',
);
expect(mockHubConnection.invoke).toHaveBeenCalledWith(
'AdicionaAoGrupoSupervisor',
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment