-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
453 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
70 changes: 70 additions & 0 deletions
70
libs/angular-google-charts/src/lib/components/chart-editor/chart-editor-ref.spec.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ChartEditorRef } from './chart-editor-ref'; | ||
|
||
const visualizationMock = { | ||
events: { | ||
addOneTimeListener: jest.fn(), | ||
removeAllListeners: jest.fn() | ||
} | ||
}; | ||
|
||
const editorMock = { | ||
openDialog: jest.fn(), | ||
closeDialog: jest.fn(), | ||
setChartWrapper: jest.fn(), | ||
getChartWrapper: jest.fn() | ||
} as jest.Mocked<google.visualization.ChartEditor>; | ||
|
||
describe('ChartEditorRef', () => { | ||
let editor: ChartEditorRef; | ||
|
||
beforeEach(() => { | ||
globalThis.google = { visualization: visualizationMock } as any; | ||
editor = new ChartEditorRef(editorMock); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(editor).toBeTruthy(); | ||
}); | ||
|
||
it('should register event listeners on create', () => { | ||
expect(visualizationMock.events.addOneTimeListener).toHaveBeenCalledWith(editorMock, 'ok', expect.any(Function)); | ||
expect(visualizationMock.events.addOneTimeListener).toHaveBeenCalledWith(editorMock, 'cancel', expect.any(Function)); | ||
}); | ||
|
||
describe('afterClosed', () => { | ||
it('should emit update wrapper if dialog was saved', () => { | ||
const okCallback = visualizationMock.events.addOneTimeListener.mock.calls[0][2]; | ||
|
||
const editResult = { draw: jest.fn() }; | ||
editorMock.getChartWrapper.mockReturnValueOnce(editResult as any); | ||
|
||
const closedSpy = jest.fn(); | ||
editor.afterClosed().subscribe(result => closedSpy(result)); | ||
|
||
okCallback(); | ||
|
||
expect(google.visualization.events.removeAllListeners).toHaveBeenCalled(); | ||
expect(closedSpy).toHaveBeenCalledWith(editResult); | ||
}); | ||
|
||
it('should emit `null` if dialog was cancelled', () => { | ||
const cancelCallback = visualizationMock.events.addOneTimeListener.mock.calls[1][2]; | ||
|
||
const closedSpy = jest.fn(); | ||
editor.afterClosed().subscribe(result => closedSpy(result)); | ||
|
||
cancelCallback(); | ||
|
||
expect(google.visualization.events.removeAllListeners).toHaveBeenCalled(); | ||
expect(closedSpy).toHaveBeenCalledWith(null); | ||
}); | ||
}); | ||
|
||
describe('cancel', () => { | ||
it('should close the dialog', () => { | ||
editor.cancel(); | ||
|
||
expect(editorMock.closeDialog).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
libs/angular-google-charts/src/lib/components/chart-editor/chart-editor-ref.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Observable, Subject } from 'rxjs'; | ||
|
||
export type EditChartResult = google.visualization.ChartWrapper | null; | ||
|
||
export class ChartEditorRef { | ||
private readonly doneSubject = new Subject<EditChartResult>(); | ||
|
||
constructor(private readonly editor: google.visualization.ChartEditor) { | ||
this.addEventListeners(); | ||
} | ||
|
||
/** | ||
* Gets an observable that is notified when the dialog is saved. | ||
* Emits either the result if the dialog was saved or `null` if editing was cancelled. | ||
*/ | ||
public afterClosed(): Observable<EditChartResult> { | ||
return this.doneSubject.asObservable(); | ||
} | ||
|
||
/** | ||
* Stops editing the chart and closes the dialog. | ||
*/ | ||
public cancel() { | ||
this.editor.closeDialog(); | ||
} | ||
|
||
private addEventListeners() { | ||
google.visualization.events.addOneTimeListener(this.editor, 'ok', () => { | ||
google.visualization.events.removeAllListeners(this.editor); | ||
|
||
const updatedChartWrapper = this.editor.getChartWrapper(); | ||
|
||
this.doneSubject.next(updatedChartWrapper); | ||
this.doneSubject.complete(); | ||
}); | ||
|
||
google.visualization.events.addOneTimeListener(this.editor, 'cancel', () => { | ||
google.visualization.events.removeAllListeners(this.editor); | ||
|
||
this.doneSubject.next(null); | ||
this.doneSubject.complete(); | ||
}); | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
libs/angular-google-charts/src/lib/components/chart-editor/chart-editor.component.spec.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { EMPTY, of } from 'rxjs'; | ||
|
||
import { ScriptLoaderService } from '../../script-loader/script-loader.service'; | ||
import { ChartBase } from '../chart-base/chart-base.component'; | ||
|
||
import { ChartEditorRef } from './chart-editor-ref'; | ||
import { ChartEditorComponent } from './chart-editor.component'; | ||
|
||
jest.mock('../../script-loader/script-loader.service'); | ||
jest.mock('./chart-editor-ref'); | ||
|
||
const editorRefMock = { | ||
afterClosed: jest.fn() | ||
}; | ||
|
||
const visualizationMock = { | ||
ChartEditor: jest.fn() | ||
}; | ||
|
||
const editorMock = { | ||
openDialog: jest.fn(), | ||
closeDialog: jest.fn(), | ||
setChartWrapper: jest.fn(), | ||
getChartWrapper: jest.fn() | ||
} as jest.Mocked<google.visualization.ChartEditor>; | ||
|
||
describe('ChartEditorComponent', () => { | ||
let component: ChartEditorComponent; | ||
let fixture: ComponentFixture<ChartEditorComponent>; | ||
|
||
beforeEach(() => { | ||
visualizationMock.ChartEditor.mockReturnValue(editorMock); | ||
globalThis.google = { visualization: visualizationMock } as any; | ||
}); | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ChartEditorComponent], | ||
providers: [ScriptLoaderService] | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ChartEditorComponent); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
describe('ngOnInit', () => { | ||
it('should load editor package', () => { | ||
const scriptLoaderService = TestBed.inject(ScriptLoaderService) as jest.Mocked<ScriptLoaderService>; | ||
scriptLoaderService.loadChartPackages.mockReturnValueOnce(EMPTY); | ||
|
||
component.ngOnInit(); | ||
|
||
expect(scriptLoaderService.loadChartPackages).toHaveBeenCalledWith('charteditor'); | ||
}); | ||
|
||
it('should create chart editor', () => { | ||
const scriptLoaderService = TestBed.inject(ScriptLoaderService) as jest.Mocked<ScriptLoaderService>; | ||
scriptLoaderService.loadChartPackages.mockReturnValueOnce(of(null)); | ||
|
||
component.ngOnInit(); | ||
|
||
expect(visualizationMock.ChartEditor).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should emit initialized event', () => { | ||
const scriptLoaderService = TestBed.inject(ScriptLoaderService) as jest.Mocked<ScriptLoaderService>; | ||
scriptLoaderService.loadChartPackages.mockReturnValueOnce(of(null)); | ||
|
||
const initializedSpy = jest.fn(); | ||
component.initialized$.subscribe(event => initializedSpy(event)); | ||
|
||
component.ngOnInit(); | ||
|
||
expect(initializedSpy).toHaveBeenCalledWith(editorMock); | ||
}); | ||
}); | ||
|
||
describe('editChart', () => { | ||
const chartWrapper = { draw: jest.fn() } as any; | ||
const chartComponent = {} as ChartBase; | ||
|
||
beforeEach(() => { | ||
Object.defineProperty(chartComponent, 'chartWrapper', { get: () => chartWrapper }); | ||
|
||
component['editor'] = editorMock; | ||
((ChartEditorRef as any) as jest.SpyInstance).mockReturnValue(editorRefMock); | ||
editorRefMock.afterClosed.mockReturnValue(EMPTY); | ||
}); | ||
|
||
it('should open the edit dialog', () => { | ||
component.editChart(chartComponent); | ||
|
||
expect(editorMock.openDialog).toHaveBeenCalledWith(chartComponent.chartWrapper, {}); | ||
}); | ||
|
||
it('should pass the provided options', () => { | ||
const options = { | ||
dataSourceInput: 'urlbox' | ||
} as google.visualization.ChartEditorOptions; | ||
|
||
component.editChart(chartComponent, options); | ||
|
||
expect(editorMock.openDialog).toHaveBeenCalledWith(chartComponent.chartWrapper, options); | ||
}); | ||
|
||
it('should create an editor ref and return it', () => { | ||
const handle = component.editChart(chartComponent); | ||
|
||
expect(ChartEditorRef).toHaveBeenCalledWith(editorMock); | ||
expect(handle).toBe(editorRefMock); | ||
}); | ||
|
||
it('should update the components chart wrapper with the edit result', () => { | ||
const setSpy = jest.fn(); | ||
Object.defineProperty(chartComponent, 'chartWrapper', { get: () => chartWrapper, set: setSpy }); | ||
|
||
const updatedWrapper = { draw: jest.fn() }; | ||
editorRefMock.afterClosed.mockReturnValue(of(updatedWrapper)); | ||
|
||
component.editChart(chartComponent); | ||
|
||
expect(setSpy).toHaveBeenCalledWith(updatedWrapper); | ||
}); | ||
|
||
it('should not update the components wrapper if editing was cancelled', () => { | ||
const setSpy = jest.fn(); | ||
Object.defineProperty(chartComponent, 'chartWrapper', { get: () => chartWrapper, set: setSpy }); | ||
|
||
editorRefMock.afterClosed.mockReturnValue(of(null)); | ||
|
||
component.editChart(chartComponent); | ||
|
||
expect(setSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.