Skip to content

Instantly share code, notes, and snippets.

@merlosy
merlosy / selector.ts
Last active February 9, 2024 15:31
Angular Jasmine Testing utils
import { DebugElement } from '@angular/core';
import { ComponentFixture } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
/** Find an element by integration test id
* @return a debugElement or null when not found
*/
export function findByIntId<T>(fixture: ComponentFixture<T> , intId: string): DebugElement | null {
return fixture.debugElement.query(By.css(`[data-int=${intId}]`));
}
@merlosy
merlosy / file.ts
Created April 27, 2023 20:02
File download snippets
/** Download a file from Blob and clean up memory */
export function downloadBlob(blob: Blob, filename: string | null = null): void {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('href', url);
a.setAttribute('download', filename || 'download');
document.body.appendChild(a);
// Prevent E2E from actually downloading files during test runs
if (!isE2ETesting()) {
@merlosy
merlosy / svg-transform.js
Created March 10, 2023 21:32
Jest SVG transformer
const path = require('path');
const jest = require('jest');
/**
* Object props is a more extensible solution when we have a lot of props
*/
function buildModule({ pathname }) {
return `
module.exports = {
__esModule: true,
@merlosy
merlosy / action.ts
Created March 23, 2022 21:25
Vuex class types
/**
Usage:
@Action(nameOf<ActionFn>('myAction'), { namespace: 'my-module'})
loadGraph!: ActionType<'myAction'>;
*/
function _createActions() {
return {
@merlosy
merlosy / location.ts
Created March 30, 2021 14:20
Jest testing utils
/** Mocked location object. Includes the original to rollback to the native one */
export interface MockedLocation {
/** The original location definition */
original: Location;
/** Restore the original location object */
clear(): void;
}
@merlosy
merlosy / lcov-model.js
Last active July 13, 2018 15:40
Merge multiple lcov.info report (node script)
/**
* @see http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php
*/
class Test {
constructor() {
this.FN = [];
this.FNDA = [];
this.DA = [];
this.BRDA = [];
}
@merlosy
merlosy / merge-cobertura.js
Last active February 21, 2023 12:22
Merge multiple cobertura report in XML (node script)
/**
* This is a node script to merge cobertura reports in XML format.
* It requires `xml2js` : `npm i -D xml2js`
*
* Execute with:
* ```
* node merge-cobertura.js coverage/file1.xml coverage/file2.xml to=coverage-final.xml
* ```
* @see https://github.com/Leonidas-from-XIV/node-xml2js
* @author Jérémy Legros
@merlosy
merlosy / google-maps.spec.ts
Created February 19, 2018 10:24
Google Maps *Stubs* for Javascript API
/**
* This module aims to provides stubs Google Maps (Javascript API).
* It is not comprehensive, but a working solution that allows unit testing, without the async pain.
* @example
* import * as google from './google-maps.spec';
* (<any>window).google = google;
*/
export const google = {
maps: {
ControlPosition: {
@merlosy
merlosy / nullable-radio-button.directive.ts
Last active January 23, 2018 09:13
Angular Material NullabelRadioButton
import { Directive, HostListener, OnInit, OnDestroy } from '@angular/core';
import { NgControl } from '@angular/forms';
import { combineLatest, tap, filter, take, startWith, debounceTime } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Directive({
selector: '[appNullableRadioButton]'
})
export class NullableRadioButtonDirective implements OnInit, OnDestroy {
@merlosy
merlosy / common-error.model.ts
Last active July 15, 2019 22:52
Blob to Json
blobToJson(blob: Blob): Observable<any> {
let loadend: Observable<any>;
if (blob.type === 'application/json') {
const reader = new FileReader();
loadend = fromEvent(reader, 'loadend').pipe(
map((read: any) => {
this.logger.debug('blobToJson:loadend *', JSON.parse(read.target.result));
return JSON.parse(read.target.result);
})
);