Skip to content

Instantly share code, notes, and snippets.

View gparlakov's full-sized avatar

Georgi Parlakov gparlakov

  • Sofia, Bulgaria
View GitHub Profile
export function setup() {
// common
const router = <Router>(
(<unknown>jasmine.createSpyObj('router', ['navigate']))
);
const canvas = new ElementRef(document.createElement('canvas'));
// the chart builder
const chartBuilder = jasmine.createSpy('chart builder');
describe('test my chart', () => {
it('should draw the chart on ngAfterViewInit', () => {
// arrange
const c = setup().build();
// act
c.ngAfterViewInit();
// assert
expect(c.chart).toBeDefined();
});
it('when clicked on the second point should NOT navigate to show the selected point if it is NOT visible', () => {
// arrange
const r = mockRouter();
// arrange the onclick prerequisites
const ch = jasmine.createSpy('chart builder');
let onClickCallback: Function = () => {};
ch.and.callFake((i, o) => {
onClickCallback = o.options.onClick;
return {
// mock the isVisible method
@gparlakov
gparlakov / test-chart-js.callback-calls-routre-method.ts
Last active May 18, 2022 18:59
test-chart-js.callback-calls-routre-method.ts
it('when clicked on the second point should navigate to show the selected point', () => {
// arrange
const r = mockRouter();
// arrange the onclick prerequisites
const ch = jasmine.createSpy('chart builder');
let onClickCallback: Function = () => {};
ch.and.callFake((i, o) => {
onClickCallback = o.options.onClick;
return {}; // <-- mock needed chart methods and props here
});
@gparlakov
gparlakov / test-chart-js.chart-builder-called-chart-constructed.ts
Last active May 18, 2022 18:55
test-chart-js.chart-builder-called-chart-constructed.ts
it('should draw the chart on ngAfterViewInit with canvas and config', () => {
// arrange
const r = mockRouter();
const builderSpy = jasmine.createSpy('chart builder')
builderSpy.and.returnValue('the chart');
const c = new ChartComponent(r, builderSpy);
const canvas = document.createElement('canvas');
c.canvas = new ElementRef(canvas);
// act
c.ngAfterViewInit();
@Component({
template: `<canvas #canvas width="200" height="200"></canvas>`,
})
export class ChartComponent {
@ViewChild('canvas')
canvas: ElementRef | undefined;
chart: Chart | undefined;
constructor(private router: Router) {}
@gparlakov
gparlakov / chart-js-wrap.ts
Last active May 22, 2022 16:29
Wrap chart js constructor in a function and provide it with an InjectionToken
export function chartBuilder(item: ChartItem, options: ChartConfiguration) {
return new Chart(item, options);
}
// The 👆 function and the Token 👇
const ChartBuilderToken = new InjectionToken<typeof chartBuilder>(
'The Chart.js instance builder'
);
@Component({
...,
import {
Component,
ElementRef,
Inject,
InjectionToken,
ViewChild,
} from '@angular/core';
import { Router } from '@angular/router';
import { Chart, ChartConfiguration, ChartItem } from 'chart.js';
@gparlakov
gparlakov / __name@dasherize__.function-spec.ts.template
Last active November 18, 2021 05:09
A function template example for scuri (use with config or --functionTemplate my/path/to/function.template)
This template demostrates how to apply functions to the file name.
Functions from https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/core/src/utils/strings.ts
@gparlakov
gparlakov / ifFeatureFlag-else.html
Last active October 20, 2021 11:37
IfFeatureFlag: support for else case
<div *ifFeature="'myNewFeatureFlagOn'; else dontShowMyFeature">My new feature (beta)</div>
<ng-template #dontShowMyFeature>Coming soon...</ng-template>