Best JavaScript code snippet using storybook-root
file.test.ts
Source:file.test.ts
2import * as File from '../../file';3suite('File Test Suite', function () {4 suite('sanitizeName', function () {5 test('trims', function () {6 assert.strictEqual(File.sanitizeName(' \n trim-test.txt \n ', 'posix'), 'trim-test.txt');7 });8 test('removes control chars', function () {9 const c0 = String.fromCharCode(...Array(32).keys());10 const c1 = Array.from(Array(32), (_v, k) => String.fromCharCode(k + 128));11 const del = String.fromCharCode(127);12 const input = ['c0', ...c0, 'c1', ...c1, 'del', ...del, '.txt'].join('');13 14 assert.strictEqual(File.sanitizeName(input, 'posix'), 'c0c1del.txt');15 });16 test('removes trailing periods and spaces', function () {17 assert.strictEqual(File.sanitizeName(' ....test.txt .. .. ', 'posix'), '....test.txt');18 });19 test('removes reserved chars with removeReservedChars option', function () {20 const options = { removeReservedChars: true };21 assert.strictEqual(File.sanitizeName('_<_>_:_"_/_\\_|_?_*_.txt', 'posix', options), '__________.txt');22 });23 test('replaces reserved chars', function () {24 assert.strictEqual(File.sanitizeName('_<_>_:_"_/_\\_|_?_*_.txt', 'posix'), '_-_-_-_-_-_-_-_-_-_.txt');25 });26 test('returns empty string for "." as a name', function () {27 assert.strictEqual(File.sanitizeName('.', 'posix'), '');28 });29 test('returns empty string for ".." as a name', function () {30 assert.strictEqual(File.sanitizeName('..', 'posix'), '');31 });32 test('prefixes Windows reserved filenames when a target platform is "win32"', function () {33 const windowsReservedNames = [34 'CON', 'PRN', 'AUX', 'NUL', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9',35 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9'36 ];37 for (const name of windowsReservedNames) {38 let badFilename = `${name} .txt`;39 let goodFilename = `${name}-.txt`;40 assert.strictEqual(File.sanitizeName(badFilename, 'win32'), `_${badFilename}`, name);41 assert.strictEqual(File.sanitizeName(goodFilename, 'win32'), goodFilename, name);42 }43 });44 test('ignores Windows reserved filenames when a target platform is not "win32"', function () {45 const windowsReservedNames = [46 'CON', 'PRN', 'AUX', 'NUL', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9',47 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9'48 ];49 for (const name of windowsReservedNames) {50 let badFilename = `${name} .txt`;51 let goodFilename = `${name}-.txt`;52 assert.strictEqual(File.sanitizeName(badFilename, 'posix'), badFilename, name);53 assert.strictEqual(File.sanitizeName(goodFilename, 'posix'), goodFilename, name);54 }55 });56 test('considers path as a part of the name', function () {57 assert.strictEqual(File.sanitizeName(' \n /dir1\\dir2/CON.txt \n ', 'posix'), '-dir1-dir2-CON.txt');58 });59 });60 suite('sanitizePath', function () {61 test('trims', function () {62 assert.strictEqual(File.sanitizePath(' \n /dir1/dir2/test.txt \n ', 'posix'), '/dir1/dir2/test.txt');63 });64 test('sanitizes segments', function () {65 assert.strictEqual(File.sanitizePath('/d<ir1/d>ir2/te*st.txt', 'posix'), '/d-ir1/d-ir2/te-st.txt');66 });67 test('transforms path using target platform\'s separator', function () {68 assert.strictEqual(File.sanitizePath('/dir1/dir2/test.txt', 'posix', 'win32'), '\\dir1\\dir2\\test.txt');69 });70 test('transforms Windows drive letters into dirs', function () {71 assert.strictEqual(File.sanitizePath('C:\\d*ir1\\d:ir2\\CON.txt', 'win32'), 'C\\d-ir1\\d-ir2\\_CON.txt');...
machineQueries.js
Source:machineQueries.js
...7const uri = "http://localhost:3333/graphql"8const link = createHttpLink({ uri, fetch })9export const createMachine = async (record) => {10 return new Promise(async (resolve, reject) => {11 if (sanitizeName(record.MACHINERY) != undefined) {12 let machineQueryResults = await getMachine(record)13 machineQueryResults.subscribe({14 next: (data) => {15 if (data.data.machine != null) {16 console.log(`${sanitizeName(record.MACHINERY)} already on db`)17 resolve(getMachine(record))18 } else {19 console.log(`${sanitizeName(record.MACHINERY)} not on db`)20 const gqlmutation = {21 query: gql`22 mutation createMachine($input: MachineInput) {23 CreateMachine(machine: $input) {24 id25 name26 model27 serialnumber28 }29 }30 `,31 variables: {32 input: {33 name: sanitizeName(record.MACHINERY),34 details: sanitizeName(record.DETAILS),35 model: sanitizeName(record.MODEL),36 serialnumber: sanitizeName(record.SN, true),37 },38 },39 }40 resolve(execute(link, gqlmutation))41 }42 },43 error: (error) => console.log(`received error ${JSON.stringify(error, null, 2)}`),44 complete: () => {},45 })46 }47 })48}49// Since this is an async function I can't give a result until it is resolved, so I'll return the observable and subscribe on it.50export const getMachine = async (record) => {51 if (sanitizeName(record.MACHINERY) != undefined) {52 const machinequery = {53 query: gql`54 query machine($name: String!) {55 machine(name: $name) {56 id57 name58 }59 }60 `,61 variables: {62 name: sanitizeName(record.MACHINERY),63 },64 }65 return execute(link, machinequery)66 }67 return undefined...
name.js
Source:name.js
...6 case 0:7 case 1:8 return ["invalid", "invalid", "invalid", "invalid"];9 case 2:10 return [sanitizeName(nameArray[0]), "", "", sanitizeName(nameArray[1])];11 case 3:12 return [13 sanitizeName(nameArray[0]),14 sanitizeName(nameArray[1]),15 "",16 sanitizeName(nameArray[2]),17 ];18 case 4:19 return [20 sanitizeName(nameArray[0]),21 sanitizeName(nameArray[1]),22 sanitizeName(nameArray[2]),23 sanitizeName(nameArray[3]),24 ];25 default:26 return [27 sanitizeName(nameArray[0]),28 sanitizeName(nameArray[1]),29 sanitizeName(nameArray.slice(2, nameArray.length - 1).join(" ")),30 sanitizeName(_.last(nameArray)),31 ];32 }33};34const sanitizeName = (name) => {35 if (!name) return "invalid";36 return name.replace(/-/g, " ");37};...
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!!