UNPKG

@twec/node-suite

Version:

Generic functionality for connecting to NetSuite Web Services from Node

91 lines (80 loc) 3.73 kB
const test = require('ava'); const NetSuite = require('../lib/index'); test.beforeEach('instantiate netsuite', (t) => { const { context } = t; context.ns = new NetSuite(); context.sampleFilePath = './tests/test-file.csv'; context.sampleFileContent = 'SGVsbG8sV29ybGQKMSwyCjMsNAo1LDYK'; }); test('netSuite config has the appropriate properties by default', (t) => { const { config } = t.context.ns; t.is(typeof config.concurrency, 'number'); t.is(typeof config.version, 'string'); t.is(typeof config.domain, 'string'); t.is(typeof config.passport.account, 'string'); t.is(typeof config.oauth.consumer.key, 'string'); t.is(typeof config.oauth.consumer.secret, 'string'); t.is(typeof config.oauth.token.key, 'string'); t.is(typeof config.oauth.token.secret, 'string'); }); // TODO: Add tests for other methods test('readFileContent returns a string of the file\'s content', async (t) => { const { sampleFilePath, sampleFileContent } = t.context; const fileContent = await NetSuite.readFileContent(sampleFilePath); t.is(fileContent, sampleFileContent); }); test('getRecordBody returns object with attribute internalId if in param obj', (t) => { const body = NetSuite.getRecordBody({ internalId: '1234' }); t.is(body.attributes.type, undefined); t.is(body.attributes.internalId, '1234'); t.is(body.attributes.externalId, undefined); }); test('getRecordBody returns object with attribute externalId if in param obj', (t) => { const body = NetSuite.getRecordBody({ externalId: '1234' }); t.is(body.attributes.type, undefined); t.is(body.attributes.internalId, undefined); t.is(body.attributes.externalId, '1234'); }); test('getRecordBody returns object with attribute type if in param obj', (t) => { const body = NetSuite.getRecordBody({ type: 'x' }); t.is(body.attributes.type, 'x'); t.is(body.attributes.internalId, undefined); t.is(body.attributes.externalId, undefined); }); test('upload reads file content and uploads the file content', async (t) => { const { ns, sampleFilePath, sampleFileContent } = t.context; const uploadFileContentArguments = {}; ns.uploadFileContent = function mockUploadFileContent(fileContent, targetFilePath) { uploadFileContentArguments.fileContent = fileContent; uploadFileContentArguments.targetFilePath = targetFilePath; return 'TEST'; }; const sampleTargetFilePath = '/someFolder/subFolder/test.csv'; const uploadReturn = await ns.upload(sampleFilePath, sampleTargetFilePath); t.is(uploadReturn, 'TEST'); t.is(uploadFileContentArguments.fileContent, sampleFileContent); t.is(uploadFileContentArguments.targetFilePath, sampleTargetFilePath); }); test('uploadFileContent fetches the folder internal id', async (t) => { const { ns, sampleFilePath, sampleFileContent } = t.context; const fetchArguments = {}; ns.fetchFolderInternalId = function mockFetchFolderInternalId(path) { fetchArguments.path = path; }; ns.makeSoapRequest = async () => null; // don't actually attempt a request await ns.uploadFileContent(sampleFileContent, sampleFilePath); t.is(fetchArguments.path, sampleFilePath); }); test('uploadFileContent makes a soap request with body', async (t) => { const { ns, sampleFilePath, sampleFileContent } = t.context; ns.fetchFolderInternalId = async () => '123'; // don't actually search for the folder const soapArgs = {}; ns.makeSoapRequest = async function mockMakeSoapRequest(operation, body) { soapArgs.operation = operation; soapArgs.body = body; }; await ns.uploadFileContent(sampleFileContent, sampleFilePath); t.is(soapArgs.operation, 'upsert'); t.is(typeof soapArgs.body, 'object'); t.is(soapArgs.body.name, 'platformMsgs:recordRef'); });