Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit 7c91088

Browse files
lukaszMycskrolikowskidamian
authored andcommitted
Chore: Add test which is using define function (#15)
* adding example map/reduce * add example with define * change package version
1 parent 1aff537 commit 7c91088

File tree

6 files changed

+132
-5
lines changed

6 files changed

+132
-5
lines changed

Examples/Jest/1.0/exampleClientScript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function saveRecord() {
1919
const confirmMessage = "Do you want to auto approve this order?\nIf yes click on 'Ok' button.\nIf not please click on 'Cancel' button";
2020
// eslint-disable-next-line no-restricted-globals, no-alert
2121
if (confirm(confirmMessage)) {
22-
nlapiSetFieldValue('custbody_auto_approva_manual_ord', 'T');
22+
nlapiSetFieldValue('custbody_auto_approve_order', 'T');
2323
}
2424

2525
return true;

Examples/Jest/1.0/exampleClientScript.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('salesorder_CL', () => {
1818
'Do you want to auto approve this order?\nIf yes click on \'Ok\' button.\nIf not please click on \'Cancel\' button',
1919
);
2020
expect(spyAlert).not.toHaveBeenCalled();
21-
expect(spySet).toHaveBeenCalledWith('custbody_auto_approva_manual_ord', 'T');
21+
expect(spySet).toHaveBeenCalledWith('custbody_auto_approve_order', 'T');
2222
expect(result).toEqual(true);
2323
});
2424
it('should not display confirm and retun true for subsidiary 1 ', () => {

Examples/Jest/2.0/exampleMapReduce.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @NApiVersion 2.0
3+
* @NScriptType MapReduceScript
4+
*/
5+
6+
/**
7+
* This is an example of Map/Reduce script file written in SuiteScript 2.0
8+
* In corresponding .test.js file you can find a way how to test it
9+
* of course with our ns-suitescript-mocks. This file needs to run through babel compilation
10+
* in order to be used in NEtSuite as it is using ES6
11+
*/
12+
13+
// Line below is required only becasue we are in the same repository as the library,
14+
// normally define is added to globally once you import our library and follow setup instructions
15+
const { define } = require('../../..')({});
16+
17+
// eslint-disable-next-line import/no-amd
18+
define(['N/record', 'N/log', 'N/https', 'N/runtime', 'lodash'],
19+
20+
(record, log, https, runtime, _) => {
21+
/**
22+
* @returns {Object}
23+
*/
24+
function getInputData() {
25+
const baseUrl = runtime.envType === 'SANDBOX' ? 'https://test.com/' : 'https://production.com/';
26+
const requestData = {
27+
url: `${baseUrl}api/v2/tickets.json`,
28+
headers: {
29+
'Content-Type': 'application/json',
30+
Authorization: 'Basic 1234567890',
31+
},
32+
};
33+
const result = https.get(requestData);
34+
const urls = [];
35+
if (result.body && JSON.parse(result.body).count) {
36+
const count = parseInt(JSON.parse(result.body).count, 10);
37+
const pages = _.ceil(count / 100);
38+
for (let index = 1; index <= pages; index += 1) {
39+
urls.push(`${baseUrl}api/v2/tickets.json?page=${index}`);
40+
}
41+
}
42+
return urls;
43+
}
44+
45+
46+
/**
47+
*
48+
* @param {object} context NS context for map function
49+
*/
50+
function map(context) {
51+
try {
52+
const pageURL = JSON.parse(context.value).toString();
53+
const requestData = {
54+
url: pageURL.toString(),
55+
headers: {
56+
'Content-Type': 'application/json',
57+
Authorization: 'Basic 1234567',
58+
},
59+
};
60+
const result = https.get(requestData);
61+
const parsedBody = JSON.parse(result.body);
62+
if (result && parsedBody && parsedBody.tickets) {
63+
const { tickets } = parsedBody;
64+
tickets.forEach((ticket) => {
65+
if (ticket.external_id) {
66+
record.submitFields({
67+
type: record.Type.SUPPORT_CASE,
68+
id: ticket.external_id,
69+
values: {
70+
custevent_page_id: ticket.id,
71+
},
72+
});
73+
}
74+
});
75+
}
76+
} catch (error) {
77+
log.error('Error in map', error);
78+
}
79+
}
80+
// required for node
81+
if (typeof module !== 'undefined' && module.exports) {
82+
module.exports = {
83+
getInputData,
84+
map,
85+
};
86+
}
87+
return {
88+
getInputData,
89+
map,
90+
};
91+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* This is a test file which shows you alternative way of getting modules in test
3+
* If you need some module in the test iteself you can either load it with require,
4+
* or simple use define function to load require modules
5+
*/
6+
7+
const { define } = require('../../..')({});
8+
const fileUnderTest = require('./exampleMapReduce');
9+
// eslint-disable-next-line import/no-amd
10+
define(['N/record', 'N/https'],
11+
12+
(record, https) => {
13+
describe('exampleMapReduce.js', () => {
14+
describe('getInputData', () => {
15+
it('should return array', () => {
16+
const result = fileUnderTest.getInputData();
17+
expect(result).toEqual(expect.any(Array));
18+
});
19+
});
20+
describe('map', () => {
21+
it('should call submitFileds for each ticket', () => {
22+
const context = { value: random(1, 10) };
23+
const tickets = [...Array(random(1, 10))].map(() => ({
24+
external_id: random(1, 1000),
25+
id: random(1, 1000),
26+
}));
27+
const spyGet = spyOn(https, 'get').and.returnValue({ body: JSON.stringify({ tickets }) });
28+
const spySubmitFields = spyOn(record, 'submitFields').and.returnValue(tickets);
29+
fileUnderTest.map(context);
30+
expect(spyGet).toHaveBeenCalledWith(expect.any(Object));
31+
expect(spySubmitFields).toHaveBeenCalledTimes(tickets.length);
32+
});
33+
});
34+
});
35+
});

Examples/Mocha/2.0/exampleClientScript.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55

66
/**
7-
* This is an example of client script file.
7+
* This is an example of client script file written in SuiteScript 2.0
88
* In corresponding .test.js file you can find a way how to test it
9-
* of course with our ns-suitescript-mocks
9+
* of course with our ns-suitescript-mocks. This file needs to run through babel compilation
10+
* in order to be used in NEtSuite as it is using ES6
1011
*/
1112

1213
// Line below is required only becasue we are in the same repository as the library,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ns-suitescript-mocks",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Collection of mocks that can be used to improve unit-tests for SuiteScript 2.0.",
55
"main": "index.js",
66
"author": "Damian Krolikowski",

0 commit comments

Comments
 (0)