Last active
September 30, 2020 07:19
-
-
Save bampakoa/25913b83714d8123a61bc437c9a368d6 to your computer and use it in GitHub Desktop.
Mock ipcRenderer with ngx-electron using RxJS Observables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
import { ElectronService } from 'ngx-electron'; | |
import { AppComponent } from './app.component'; | |
import { FakeElectronService } from './fake-electron.service'; | |
describe('AppComponent', () => { | |
let fixture: ComponentFixture<AppComponent>; | |
let comp: AppComponent; | |
let electronService: FakeElectronService; | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
declarations: [AppComponent], | |
providers: [ | |
{ provide: ElectronService, useClass: FakeElectronService } | |
] | |
}); | |
fixture = TestBed.createComponent(AppComponent); | |
comp = fixture.componentInstance; | |
electronService = TestBed.get(ElectronService); | |
}); | |
it('should create the component', () => { | |
expect(comp).toBeTruthy(); | |
}); | |
it('should show Hello World', () => { | |
electronService.channelSource.next({ | |
channel: 'asynchronous-reply', | |
params: ['Hello World'] | |
}); | |
expect(comp.message).toEqual('Hello World'); | |
}); | |
it('should send a message', () => { | |
const spy = spyOn(electronService.ipcRenderer, 'send'); | |
comp.sendMessage('Hello World'); | |
const args = spy.calls.mostRecent().args; | |
expect(args[0]).toBe('sendMessage'); | |
expect(args[1]).toEqual('Hello World'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
import { ElectronService } from 'ngx-electron'; | |
@Component({ | |
selector: 'app-root', | |
template: '<div>{{message}}</div>' | |
}) | |
export class AppComponent { | |
message: string; | |
constructor(private electronService: ElectronService) { | |
this.electronService.ipcRenderer.on('asynchronous-reply', (event: Electron.IpcMessageEvent, arg: string) => { | |
this.message = arg; | |
}); | |
} | |
sendMessage(msg: string) { | |
this.electronService.ipcRenderer.send('sendMessage', msg); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TestBed } from '@angular/core/testing'; | |
import { ElectronService } from 'ngx-electron'; | |
import { CoreService } from './core.service'; | |
import { FakeElectronService } from './fake-electron.service'; | |
describe('CoreService', () => { | |
let service: CoreService; | |
let electronService: FakeElectronService; | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
providers: [ | |
{ provide: ElectronService, useClass: FakeElectronService } | |
] | |
}); | |
service = TestBed.get(CoreService); | |
electronService = TestBed.get(ElectronService); | |
}); | |
it('should be created', () => { | |
expect(service).toBeTruthy(); | |
}); | |
it('should get a message', (done: DoneFn) => { | |
const promise = service.getMessage(); | |
electronService.channelSource.next({ | |
channel: 'getMessage', | |
params: ['Hello World'] | |
}); | |
promise.then(msg => { | |
expect(msg).toBe('Hello World'); | |
done(); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { ElectronService } from 'ngx-electron'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class CoreService { | |
constructor(private electronService: ElectronService) { } | |
getMessage(): Promise<string> { | |
return new Promise(resolve => { | |
this.electronService.ipcRenderer.once('getMessage', (event: Electron.IpcMessageEvent, msg: string) => { | |
resolve(msg); | |
}); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Subject } from 'rxjs'; | |
class Channel { | |
constructor(public name: string, public listener: Function) {} | |
} | |
export class Message { | |
channel: string; | |
params?: any[]; | |
} | |
export class FakeElectronService { | |
channelSource = new Subject<Message>(); | |
private channels: Channel[] = []; | |
ipcRenderer = { | |
on: (name: string, listener: Function) => { | |
this.channels.push(new Channel(name, listener)); | |
}, | |
once: (name: string, listener: Function) => { | |
this.channels.push(new Channel(name, listener)); | |
}, | |
send: (channel: string, args: string) => {} | |
}; | |
constructor() { | |
this.channelSource.subscribe(msg => { | |
this.channels.find(channel => channel.name === msg.channel).listener({}, ...msg.params); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment