Skip to content

Instantly share code, notes, and snippets.

View gparlakov's full-sized avatar

Georgi Parlakov gparlakov

  • Sofia, Bulgaria
View GitHub Profile
@gparlakov
gparlakov / delete-stale-branches.sh
Last active July 17, 2023 02:29 — forked from manofearth/delete-stale-branches.sh
Delete Stale Branches
#!/usr/bin/env bash
while getopts "d" opt; do
case $opt in
d) dryRunOpt="--dry-run";;
esac
done
# prune local "cache" of remote branches first:
git fetch --prune origin
# delete merged to master branches:
@gparlakov
gparlakov / add.sh
Last active April 24, 2023 13:12
Add angular apps of a specific version
# these versions are current as of May 18th 2021
# info from https://www.npmjs.com/package/@angular/cli -> Versions
#Angular 2: looks like this is the last RC version before switching to angular 4
npx -p @angular/cli@1.0.0-rc.2 ng new angular2app
#Angular 4: the last CLI version before Angular 5
npx -p @angular/cli@1.4.10 ng new angular4app
#Angular 5: the last CLI version before Angular 6
@gparlakov
gparlakov / subscribe-in-tests.ts
Last active May 28, 2022 18:10
A helper that takes an observable and subscribes to it, returning an array that will get updated/mutated with the new values emitted. Accepts a count of values to listen to and emit before closing the subscription OR another observable signalling stop when it emits
import { Observable, isObservable } from 'rxjs';
import { takeUntil, take } from 'rxjs/operators';
/**
* A helper function that subscribes to observable and aggregates the values and errors in an array for ease of use.
*
* @param to$ The observable to subscribe to
* @param untilOrTimes The subscription ending count or other observable to signal the end of subscription
* @example
* // arrange
describe('test my chart', () => {
it('should draw the chart on ngAfterViewInit', () => {
// arrange
const c = setup().build();
// act
c.ngAfterViewInit();
// assert
expect(c.chart).toBeDefined();
});
@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({
...,
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');
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) {}