Best JavaScript code snippet using ng-mocks
csv-transform.ts
Source:csv-transform.ts
1import { ObjectId } from "mongodb";2import { Transform } from "stream";3interface ImportOptions {4 fields?: Array<string>;5}6interface CsvCell {7 header: string;8 value: string;9}10type Chunk = Record<string, any> | number | string | boolean | Date | ObjectId;11const formatHeaders = (headers: Set<string>, delim = ",") => [...headers].join(delim) + "\n";12const formatRow = (headers: Set<string>, cells: CsvCell[], delim = ",") =>13 [...headers].map((head) => cells.find((cell) => cell.header === head)?.value || "").join(delim) + "\n";14const addCells = (15 document: Chunk,16 cells: CsvCell[] = [],17 headers = new Set(),18 path: string[] = [],19 onlyHeaders = false,20) => {21 const add = (header: string, value: Chunk) => {22 if (onlyHeaders) {23 if (headers.has(header))24 cells.push({25 header,26 value: String(value),27 });28 } else {29 headers.add(header);30 cells.push({31 header,32 value: String(value),33 });34 }35 };36 if (37 typeof document === "number" ||38 typeof document === "string" ||39 typeof document === "boolean" ||40 document instanceof Date ||41 (typeof document !== "object" && ObjectId.isValid(document)) ||42 document == null43 ) {44 return add(path.join("."), document);45 }46 for (const [key, value] of Object.entries(document)) {47 if (Array.isArray(value)) {48 value.forEach((x, idx) => addCells(x, cells, headers, [...path, String(key), String(idx)], onlyHeaders));49 } else if (typeof value === "object" && !(value instanceof Date)) {50 addCells(value, cells, headers, [...path, String(key)], onlyHeaders);51 } else if (52 typeof value === "number" ||53 typeof value === "string" ||54 typeof value === "boolean" ||55 value instanceof Date ||56 (typeof document !== "object" && ObjectId.isValid(document)) ||57 document == null58 ) {59 const header = [...path, String(key)].join(".");60 add(header, value);61 }62 }63};64export const CSVTransform = (options: ImportOptions) => {65 const headers = new Set<string>(!!options.fields?.length ? options.fields : []);66 return new Transform({67 objectMode: true,68 flush(callback) {69 callback(undefined, { header: formatHeaders(headers) });70 },71 transform(chunk: Record<string, any>, encoding: string, callback: (err?: Error, value?: { row: string }) => void) {72 try {73 const cells: CsvCell[] = [];74 addCells(chunk, cells, headers, [], !!options.fields?.length);75 callback(undefined, { row: formatRow(headers, cells) });76 } catch (err: any) {77 callback(err);78 }79 },80 });...
ReportList.js
Source:ReportList.js
1import React, { PropTypes, Component } from 'react';2import Immutable from 'immutable';3import { HelpBlock } from 'react-bootstrap';4import List from '../List';5import Pager from '../EntityList/Pager';6import { ReportDescription } from '../../FieldDescriptions';7import { getConfig } from '../../common/Util';8class ReportList extends Component {9 static propTypes = {10 items: PropTypes.instanceOf(Immutable.List),11 fields: PropTypes.instanceOf(Immutable.List),12 size: PropTypes.number,13 page: PropTypes.number,14 nextPage: PropTypes.bool,15 onlyHeaders: PropTypes.bool,16 onChangePage: PropTypes.func,17 onChangeSize: PropTypes.func,18 }19 static defaultProps = {20 items: Immutable.List(),21 fields: Immutable.List(),22 size: getConfig(['list', 'defaultItems'], 10),23 page: 0,24 nextPage: false,25 onlyHeaders: false,26 onChangePage: () => {},27 onChangeSize: () => {},28 }29 shouldComponentUpdate(nextProps) {30 const { items, fields, page, nextPage, size, onlyHeaders } = this.props;31 return (32 !Immutable.is(items, nextProps.items)33 || !Immutable.is(fields, nextProps.fields)34 || size !== nextProps.size35 || page !== nextProps.page36 || nextPage !== nextProps.nextPage37 || onlyHeaders !== nextProps.onlyHeaders38 );39 }40 render() {41 const { items, size, page, nextPage, fields, onlyHeaders } = this.props;42 return (43 <div className="report-list">44 <List45 items={onlyHeaders ? null : items}46 fields={fields.toJS()}47 />48 {!onlyHeaders && (49 <Pager50 page={page}51 size={size}52 count={items.size}53 nextPage={nextPage}54 onChangePage={this.props.onChangePage}55 onChangeSize={this.props.onChangeSize}56 />57 )}58 {onlyHeaders && (<HelpBlock>{ReportDescription.block_preview}</HelpBlock>)}59 </div>60 );61 }62}...
dynamics.js
Source:dynamics.js
1var dynamics = (function() {2 function applyDynamics(tokens) {3 var tokenSlides = divideIntoSlides(tokens);4 5 for (var i in tokenSlides) {6 applyOnlyHeaders(tokenSlides[i]);7 }8 9 return mergeIntoTokens(tokenSlides);10 }11 12 function divideIntoSlides(tokens) {13 var tokenSlides = new Array();14 tokenSlides.push(new Array());15 var j = 0;16 17 for (var i in tokens) {18 tokenSlides[j].push(tokens[i])19 if (tokens[i].token == "slidemark") {20 tokenSlides.push(new Array());21 j++;22 }23 }24 25 return tokenSlides;26 }27 28 function mergeIntoTokens(tokenSlides) {29 var tokens = new Array();30 31 for (var i in tokenSlides) {32 for (var j in tokenSlides[i]) {33 tokens.push(tokenSlides[i][j]);34 }35 }36 37 return tokens;38 }39 40 function applyOnlyHeaders(tokenSlide) {41 var onlyHeaders = true;42 43 for (var i in tokenSlide) {44 var token = tokenSlide[i];45 if (token.token != "heading" && token.token != "empty" && token.token != "slidemark") {46 onlyHeaders = false;47 }48 }49 50 if (onlyHeaders) {51 for (var i in tokenSlide) {52 var token = tokenSlide[i];53 if (token.token == "heading") {54 addClass(token, "titleslide");55 }56 }57 }58 }59 60 function addClass(token, clazz) {61 if (token.addClass != undefined) {62 token.addClass += " ";63 } else {64 token.addClass = "";65 }66 token.addClass += clazz;67 }68 69 return {70 applyDynamics: applyDynamics71 };...
Using AI Code Generation
1import { onlyHeaders } from 'ng-mocks';2import { MockBuilder, MockRender } from 'ng-mocks';3import { MockModule } from 'ng-mocks';4import { MockRender } from 'ng-mocks';5import { MockService } from 'ng-mocks';6import { MockedComponentFixture } from 'ng-mocks';7import { MockedDebugElement } from 'ng-mocks';8import { MockedDirective } from 'ng-mocks';9import { MockedHostComponent } from 'ng-mocks';10import { MockedInjector } from 'ng-mocks';11import { MockedModule } from 'ng-mocks';12import { MockedPipe } from 'ng-mocks';13import { MockedProvider } from 'ng-mocks';14import { MockedRender } from 'ng-mocks';15import { MockedService } from 'ng-mocks';16import { MockedTestBed } from 'ng-mocks';17import { MockedType } from 'ng-mocks';18import { MockInstance } from 'ng-mocks';19import { MockedDebugElement } from 'ng-mocks';20import { MockedDirective } from 'ng-mocks';21import { MockedHostComponent } from 'ng-mocks';22import { MockedInjector } from 'ng-mocks';23import { MockedModule } from 'ng-mocks';24import { MockedPipe } from 'ng-mocks';25import { MockedProvider } from 'ng-mocks';
Using AI Code Generation
1import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';2import {TestBed} from '@angular/core/testing';3import {HttpClient} from '@angular/common/http';4describe('HttpClient testing', () => {5 let httpClient: HttpClient;6 let httpTestingController: HttpTestingController;7 beforeEach(() => {8 TestBed.configureTestingModule({9 imports: [HttpClientTestingModule]10 });11 httpClient = TestBed.get(HttpClient);12 httpTestingController = TestBed.get(HttpTestingController);13 });14 afterEach(() => {15 httpTestingController.verify();16 });17 it('can test HttpClient.get', () => {18 const testData = {name: 'Test Data'};19 expect(data).toEqual(testData)20 );21 expect(req.request.method).toEqual('GET');22 req.flush(testData);23 httpTestingController.verify();24 });25 it('can test for 404 error', () => {26 const emsg = 'deliberate 404 error';27 data => fail('should have failed with the 404 error'),28 (error: Error) => {29 expect(error.message).toContain(emsg);30 }31 );32 req.flush(emsg, {status: 404, statusText: 'Not Found'});33 });34 it('can test for network error', () => {35 const emsg = 'simulated network error';36 data => fail('should have failed with the network error'),37 (error: Error) => {38 expect(error.message).toContain(emsg);39 }40 );41 const mockError = new ErrorEvent('Network error', {42 });43 req.error(mockError);44 });45 it('can test for network error with an error object', () => {46 const emsg = 'simulated network error';
Using AI Code Generation
1import { onlyHeaders } from 'ng-mocks';2import { HttpClient } from '@angular/common/http';3describe('HttpClient', () => {4 let http: HttpClient;5 beforeEach(() => {6 http = TestBed.get(HttpClient);7 });8 it('should return onlyHeaders', () => {9 const headers = onlyHeaders(http);10 expect(headers).toEqual({ 'Content-Type': 'application/json' });11 });12});13import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';14import { AppModule } from './app.module';15beforeEach(() => MockBuilder(AppModule));16it('should render', () => {17 const fixture = MockRender();18 const headers = ngMocks.onlyHeaders(fixture.debugElement.injector.get(HttpClient));19 expect(headers).toEqual({ 'Content-Type': 'application/json' });20});21import { NgModule } from '@angular/core';22import { BrowserModule } from '@angular/platform-browser';23import { HttpClientModule } from '@angular/common/http';24@NgModule({25 imports: [BrowserModule, HttpClientModule],26})27export class AppModule {}28import { Component } from '@angular/core';29import { HttpClient } from '@angular/common/http';30@Component({31})32export class AppComponent {33 constructor(private http: HttpClient) {}34}
Using AI Code Generation
1const { onlyHeaders } = require('ng-mocks');2const { onlyHeaders } = require('ng-mocks');3const { onlyHeaders } = require('ng-mocks');4const { onlyHeaders } = require('ng-mocks');5const { onlyHeaders } = require('ng-mocks');6const { onlyHeaders } = require('ng-mocks');7const { onlyHeaders } = require('ng-mocks');8const { onlyHeaders } = require('ng-mocks');9const { onlyHeaders } = require('ng-mocks');10const { onlyHeaders } = require('ng-mocks');11const { onlyHeaders } = require('ng-mocks');12const { onlyHeaders } = require('ng-mocks');
Using AI Code Generation
1import { onlyHeaders } from 'ng-mocks';2describe('onlyHeaders', () => {3 it('onlyHeaders', () => {4 expect(onlyHeaders()).toEqual({5 'Content-Type': 'application/json;charset=utf-8',6 });7 });8});9import { onlyHeaders } from 'ng-mocks';10describe('onlyHeaders', () => {11 it('onlyHeaders', () => {12 expect(onlyHeaders()).toEqual({13 'Content-Type': 'application/json;charset=utf-8',14 });15 });16});17import { onlyHeaders } from 'ng-mocks';18describe('onlyHeaders', () => {19 it('onlyHeaders', () => {20 expect(onlyHeaders()).toEqual({21 'Content-Type': 'application/json;charset=utf-8',22 });23 });24});25import { onlyHeaders } from 'ng-mocks';26describe('onlyHeaders', () => {27 it('onlyHeaders', () => {28 expect(onlyHeaders()).toEqual({29 'Content-Type': 'application/json;charset=utf-8',30 });31 });32});33import { onlyHeaders } from 'ng-mocks';34describe('onlyHeaders', () => {35 it('onlyHeaders', () => {36 expect(onlyHeaders()).toEqual({37 'Content-Type': 'application/json;charset=utf-8',38 });39 });40});41import { onlyHeaders } from 'ng-mocks';42describe('onlyHeaders', () => {43 it('onlyHeaders', () => {44 expect(onlyHeaders()).toEqual({45 'Content-Type': 'application/json;charset=utf-8',46 });47 });48});
Using AI Code Generation
1import { onlyHeaders } from 'ng-mocks';2import { mockBuilder, mockInstance } from 'ng-mocks';3import { MockRender } from 'ng-mocks';4import { MockBuilder } from 'ng-mocks';5import { MockInstance } from 'ng-mocks';6import { MockRender } from 'ng-mocks';7import { MockService } from 'ng-mocks';8import { MockProvider } from 'ng-mocks';9import { MockRender } from 'ng-mocks';10import { MockService } from 'ng-mocks';11import { MockProvider } from 'ng-mocks';12import { MockRender } from 'ng-mocks';13import { MockService } from 'ng-mocks';14import { MockProvider } from 'ng-mocks';15import { MockRender } from 'ng-mocks';16import { MockService } from 'ng-mocks';17import { MockProvider } from 'ng-mocks';18import { MockRender } from 'ng-mocks';19import { MockService } from 'ng-mocks';20import { MockProvider } from 'ng-mocks';21import { MockRender } from 'ng-mocks';22import { MockService } from 'ng-mocks
Using AI Code Generation
1import { onlyHeaders } from 'ng-mocks';2describe('onlyHeaders', () => {3 it('should return only the headers of the response', () => {4 const response = new HttpResponse({5 body: { name: 'John', age: 20 },6 headers: new HttpHeaders({7 }),8 });9 const headers = onlyHeaders(response);10 expect(headers).toEqual({11 });12 });13});14import { onlyHeaders } from 'ng-mocks';15describe('onlyHeaders', () => {16 it('should return only the headers of the response', () => {17 const response = new HttpResponse({18 body: { name: 'John', age: 20 },19 headers: new HttpHeaders({20 }),21 });22 const headers = onlyHeaders(response);23 expect(headers).toEqual({24 });25 });26});27import { onlyHeaders } from '
Using AI Code Generation
1const onlyHeaders = require('ng-mocks').onlyHeaders;2const headers = onlyHeaders(MyComponent);3const headers = onlyHeaders(MyComponent, 'h1');4const headers = onlyHeaders(MyComponent, 'h1', context);5const headers = onlyHeaders(MyComponent, 'h1', context, host);6const headers = onlyHeaders(MyComponent, 'h1', context, host, debugElement);7const headers = onlyHeaders(MyComponent, 'h1', context, host, debugElement, module);8const headers = onlyHeaders(MyComponent, 'h1', context, host, debugElement, module, componentFixture);9const headers = onlyHeaders(MyComponent, 'h1', context, host, debugElement, module, componentFixture, query);10const headers = onlyHeaders(MyComponent, 'h1', context, host, debugElement, module, componentFixture, query, fixture);11const headers = onlyHeaders(MyComponent, 'h1', context, host, debugElement, module, componentFixture, query, fixture, options);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!