Best JavaScript code snippet using stryker-parent
TableStorageMapper.test.ts
Source:TableStorageMapper.test.ts
1import { Constants, TableQuery } from 'azure-storage';2import TableStorageMapper from './TableStorageMapper';3import * as TableServiceAsPromisedModule from '../services/TableServiceAsPromised';4import { StorageError } from '../../../test/helpers/StorageError';5import { DashboardQuery } from './DashboardQuery';6import { Result } from './Mapper';7import { OptimisticConcurrencyError } from '../errors';8jest.mock('../services/TableServiceAsPromised');9export class FooModel {10 public partitionId!: string;11 public rowId!: string;12 public bar!: number;13 public static createPartitionKey(entity: Pick<FooModel, 'partitionId'>): string {14 return entity.partitionId;15 }16 public static createRowKey(entity: Pick<FooModel, 'rowId'>): string | undefined {17 return entity.rowId;18 }19 public static identify(entity: FooModel, partitionKeyValue: string, rowKeyValue: string): void {20 entity.partitionId = partitionKeyValue;21 entity.rowId = rowKeyValue;22 }23 public static readonly persistedFields = ['bar'] as const;24 public static readonly tableName = 'FooTable';25}26describe(TableStorageMapper.name, () => {27 const TableServiceAsPromisedModuleMocked = TableServiceAsPromisedModule as typeof import('../services/__mocks__/TableServiceAsPromised');28 const TableService = TableServiceAsPromisedModuleMocked.default;29 class TestHelper {30 public sut = new TableStorageMapper(FooModel); // this will mock the TableService because of Jest's black magic!31 }32 let helper: TestHelper;33 beforeEach(() => {34 helper = new TestHelper();35 TableService.mockClear();36 });37 describe('createTableIfNotExists', () => {38 it('should create table "FooTable"', async () => {39 TableServiceAsPromisedModuleMocked.createTableIfNotExistsMock.mockResolvedValueOnce({});40 await helper.sut.createStorageIfNotExists();41 expect(TableServiceAsPromisedModuleMocked.createTableIfNotExistsMock).toHaveBeenCalledWith('FooTable');42 });43 });44 describe('insertOrMerge', () => {45 it('should insert the given model', async () => {46 const expected: FooModel = {47 partitionId: 'github/owner',48 rowId: 'name',49 bar: 4250 };51 TableServiceAsPromisedModuleMocked.insertOrMergeEntityMock.mockResolvedValue({});52 await helper.sut.insertOrMerge(expected);53 expect(TableServiceAsPromisedModuleMocked.insertOrMergeEntityMock).toHaveBeenCalledWith('FooTable', {54 PartitionKey: 'github;owner',55 RowKey: 'name',56 bar: 42,57 ['.metadata']: {}58 });59 expect(expected.bar).toEqual(42);60 });61 });62 describe('findOne', () => {63 it('should retrieve the entity from storage', async () => {64 const result = createEntity();65 TableServiceAsPromisedModuleMocked.retrieveEntityMock.mockResolvedValue(result);66 await helper.sut.findOne({ partitionId: 'github/partKey', rowId: 'row/key' });67 expect(TableServiceAsPromisedModuleMocked.retrieveEntityMock).toHaveBeenCalledWith('FooTable', 'github;partKey', 'row;key');68 });69 it('should return null if it resulted in a 404', async () => {70 const error = new StorageError(Constants.StorageErrorCodeStrings.RESOURCE_NOT_FOUND);71 TableServiceAsPromisedModuleMocked.retrieveEntityMock.mockRejectedValue(error);72 const actualProject = await helper.sut.findOne({ partitionId: 'github/partKey', rowId: 'rowKey' });73 expect(actualProject).toEqual(null);74 });75 it('should return the entity', async () => {76 const expected: FooModel = { rowId: 'rowKey', partitionId: 'partKey', bar: 42 };77 TableServiceAsPromisedModuleMocked.retrieveEntityMock.mockResolvedValue(createEntity(expected, 'etagValue'));78 const actualProjects = await helper.sut.findOne({ partitionId: 'github/partKey', rowId: 'rowKey' });79 expect(actualProjects).toEqual({ model: expected, etag: 'etagValue' });80 });81 });82 describe('findAll', () => {83 it('should query the underlying storage', async () => {84 const expectedQuery = new TableQuery().where('PartitionKey eq ?', 'github;partKey');85 TableServiceAsPromisedModuleMocked.queryEntitiesMock.mockResolvedValue({ entries: [] });86 await helper.sut.findAll(DashboardQuery.create(FooModel)87 .wherePartitionKeyEquals({ partitionId: 'github/partKey' })88 );89 expect(TableServiceAsPromisedModuleMocked.queryEntitiesMock).toHaveBeenCalledWith('FooTable', expectedQuery, undefined);90 });91 it('should return the all entities', async () => {92 const expectedEntities: FooModel[] = [93 { rowId: 'rowKey', partitionId: 'partKey', bar: 142 },94 { rowId: 'rowKey2', partitionId: 'partKey2', bar: 25 }95 ];96 TableServiceAsPromisedModuleMocked.queryEntitiesMock.mockResolvedValue({ entries: expectedEntities.map(entity => createEntity(entity)) });97 const actualProjects = await helper.sut.findAll(DashboardQuery.create(FooModel)98 .wherePartitionKeyEquals({ partitionId: 'github/partKey' })99 );100 expect(actualProjects).toEqual(expectedEntities.map(model => ({ model, etag: 'foo-etag' })));101 });102 });103 describe('replace', () => {104 it('should replace entity with given etag', async () => {105 TableServiceAsPromisedModuleMocked.replaceEntityMock.mockResolvedValue({ ['.metadata']: { etag: 'next-etag' } });106 const expected: FooModel = { bar: 42, partitionId: 'partId', rowId: 'rowId' };107 const expectedResult: Result<FooModel> = { model: expected, etag: 'next-etag' };108 const result = await helper.sut.replace(expected, 'prev-etag');109 expect(result).toEqual(expectedResult);110 const expectedEntity = createRawEntity(expected, 'prev-etag');111 expect(TableServiceAsPromisedModuleMocked.replaceEntityMock).toHaveBeenCalledWith(FooModel.tableName, expectedEntity, {});112 });113 it('should throw a OptimisticConcurrencyError if the UPDATE_CONDITION_NOT_SATISFIED is thrown', async () => {114 TableServiceAsPromisedModuleMocked.replaceEntityMock.mockRejectedValue(new StorageError(Constants.StorageErrorCodeStrings.UPDATE_CONDITION_NOT_SATISFIED));115 await expect(helper.sut.replace({ bar: 24, partitionId: 'part', rowId: 'row' }, 'prev-etag')).rejects.toBeInstanceOf(OptimisticConcurrencyError);116 });117 });118 describe('insert', () => {119 it('should insert entity', async () => {120 TableServiceAsPromisedModuleMocked.insertEntityMock.mockResolvedValue({ ['.metadata']: { etag: 'next-etag' } });121 const expected: FooModel = { bar: 42, partitionId: 'partId', rowId: 'rowId' };122 const expectedResult: Result<FooModel> = { model: expected, etag: 'next-etag' };123 const result: Result<FooModel> = await helper.sut.insert(expected);124 expect(result).toEqual(expectedResult);125 expect(TableServiceAsPromisedModuleMocked.insertEntityMock).toHaveBeenCalledWith(FooModel.tableName, createRawEntity(expected), {});126 });127 it('should throw an OptimisticConcurrencyError if the entity already exists', async () => {128 TableServiceAsPromisedModuleMocked.insertEntityMock.mockRejectedValue(new StorageError(Constants.TableErrorCodeStrings.ENTITY_ALREADY_EXISTS));129 await expect(helper.sut.insert({ bar: 24, partitionId: 'part', rowId: 'row' })).rejects.toBeInstanceOf(OptimisticConcurrencyError);130 });131 });132 function createRawEntity(overrides?: Partial<FooModel>, etag?: string) {133 const foo: FooModel = {134 bar: 42,135 partitionId: 'partKey',136 rowId: 'rowKey',137 ...overrides138 };139 function metadata() {140 if (etag) {141 return {142 etag143 };144 } else {145 return {};146 }147 }148 return {149 PartitionKey: foo.partitionId,150 RowKey: foo.rowId,151 bar: foo.bar,152 ['.metadata']: metadata()153 };154 }155 function createEntity(overrides?: Partial<FooModel>, etag = 'foo-etag'): TableServiceAsPromisedModule.Entity<FooModel, 'partitionId' | 'rowId'> {156 const foo: FooModel = {157 bar: 42,158 partitionId: 'partKey',159 rowId: 'rowKey',160 ...overrides161 };162 return {163 PartitionKey: { _: foo.partitionId, $: 'Edm.String' },164 RowKey: { _: foo.rowId, $: 'Edm.String' },165 bar: { _: foo.bar, $: 'Edm.Int32' },166 ['.metadata']: {167 etag168 }169 };170 }...
Using AI Code Generation
1var TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;2var tableServiceAsPromisedModuleMocked = new TableServiceAsPromisedModuleMocked();3tableServiceAsPromisedModuleMocked.createTableIfNotExists('test').then(function(result) {4 console.log(result);5});6var TableServiceAsPromisedModule = require('azure-storage').TableServiceAsPromisedModule;7var TableServiceAsPromisedModuleMocked = function() {8 this.createTableIfNotExists = function(tableName) {9 return Promise.resolve(true);10 }11}12module.exports.TableServiceAsPromisedModuleMocked = TableServiceAsPromisedModuleMocked;13jest.mock('azure-storage');14const azure = require('azure-storage');15azure.TableServiceAsPromisedModule = jest.fn().mockImplementation(() => {16 return {17 createTableIfNotExists: jest.fn().mockImplementation(() => {18 return new Promise((resolve, reject) => {19 resolve(true);20 });21 })22 };23});24azure.TableServiceAsPromisedModule.createTableIfNotExists = jest.fn().mockImplementation(() => {25 return new Promise((resolve, reject
Using AI Code Generation
1const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;2const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;3const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;4const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;5const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;6const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;7const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;8const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;9const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;10const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;11const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;12const TableServiceAsPromisedModuleMocked = require('stryker-parent
Using AI Code Generation
1const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;2TableServiceAsPromisedModuleMocked.prototype.createTableIfNotExists = () => {3 return Promise.resolve();4};5TableServiceAsPromisedModuleMocked.prototype.createTable = () => {6 return Promise.resolve();7};8TableServiceAsPromisedModuleMocked.prototype.createTableIfNotExists = () => {9 return Promise.resolve();10};11TableServiceAsPromisedModuleMocked.prototype.createTable = () => {12 return Promise.resolve();13};14TableServiceAsPromisedModuleMocked.prototype.insertEntity = () => {15 return Promise.resolve();16};17const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;18TableServiceAsPromisedModuleMocked.prototype.createTableIfNotExists = () => {19 return Promise.resolve();20};21TableServiceAsPromisedModuleMocked.prototype.createTable = () => {22 return Promise.resolve();23};24TableServiceAsPromisedModuleMocked.prototype.createTableIfNotExists = () => {25 return Promise.resolve();26};27TableServiceAsPromisedModuleMocked.prototype.createTable = () => {28 return Promise.resolve();29};30TableServiceAsPromisedModuleMocked.prototype.insertEntity = () => {31 return Promise.resolve();32};33const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;34TableServiceAsPromisedModuleMocked.prototype.createTableIfNotExists = () => {35 return Promise.resolve();36};37TableServiceAsPromisedModuleMocked.prototype.createTable = () => {38 return Promise.resolve();39};40TableServiceAsPromisedModuleMocked.prototype.createTableIfNotExists = () => {41 return Promise.resolve();42};43TableServiceAsPromisedModuleMocked.prototype.createTable = () => {44 return Promise.resolve();45};46TableServiceAsPromisedModuleMocked.prototype.insertEntity = () => {47 return Promise.resolve();48};49const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;
Using AI Code Generation
1var TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;2var tableServiceAsPromised = new TableServiceAsPromisedModuleMocked('accountname','accountkey');3tableServiceAsPromised.createTableIfNotExists('testTable').then(function(result){4 console.log(result);5});6var TableServiceAsPromisedModuleMocked = require('./node_modules/stryker-parent').TableServiceAsPromisedModuleMocked;7var tableServiceAsPromised = new TableServiceAsPromisedModuleMocked('accountname','accountkey');8tableServiceAsPromised.createTableIfNotExists('testTable').then(function(result){9 console.log(result);10});11var strykerParent = require('stryker-parent');12var TableServiceAsPromisedModuleMocked = strykerParent.TableServiceAsPromisedModuleMocked;13var strykerParent = require('stryker-parent');14var TableServiceAsPromisedModuleMocked = strykerParent.TableServiceAsPromisedModuleMocked;
Using AI Code Generation
1var TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;2var tableService = TableServiceAsPromisedModuleMocked.createTableService("mocked account name", "mocked account key");3tableService.createTableIfNotExists("mocked table name", function(error, result, response) {4 if (!error) {5 }6});7var TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;8var tableService = TableServiceAsPromisedModuleMocked.createTableService("mocked account name", "mocked account key");9tableService.createTableIfNotExists("mocked table name", function(error, result, response) {10 if (!error) {11 }12});13var TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;14var tableService = TableServiceAsPromisedModuleMocked.createTableService("mocked account name", "mocked account key");15tableService.createTableIfNotExists("mocked table name", function(error, result, response) {16 if (!error) {17 }18});19var TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;20var tableService = TableServiceAsPromisedModuleMocked.createTableService("mocked account name", "mocked account key");21tableService.createTableIfNotExists("mocked table name", function(error, result, response) {22 if (!error) {23 }24});25var TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;26var tableService = TableServiceAsPromisedModuleMocked.createTableService("mocked
Using AI Code Generation
1import TableServiceAsPromisedModuleMocked from 'stryker-parent';2import TableServiceAsPromisedModuleMocked from 'stryker-parent';3import TableServiceAsPromisedModuleMocked from 'stryker-parent';4import TableServiceAsPromisedModuleMocked from 'stryker-parent';5import TableServiceAsPromisedModuleMocked from 'stryker-parent';6import TableServiceAsPromisedModuleMocked from 'stryker-parent';7import TableServiceAsPromisedModuleMocked from 'stryker-parent';8import TableServiceAsPromisedModuleMocked from 'stryker-parent';9import TableServiceAsPromisedModuleMocked from '
Using AI Code Generation
1const TableServiceAsPromisedModuleMocked = require('stryker-parent').TableServiceAsPromisedModuleMocked;2const tableService = TableServiceAsPromisedModuleMocked.createTableServiceAsPromisedMock('useDevelopmentStorage=true;');3tableService.createTableIfNotExists = jest.fn().mockImplementation(() => {4 return new Promise((resolve, reject) => {5 resolve();6 });7});8test('createTableIfNotExists', () => {9 return tableService.createTableIfNotExists('test').then(() => {10 expect(tableService.createTableIfNotExists).toBeCalled();11 });12});
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!!