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

Commit f06d4ab

Browse files
authored
Chore: Add Examples (#9)
* changes * increase code coverage and create utils library * add examples for 1.0 and 2.0 * cleanup examples and update readme * change module version * change version * fix lint warnings
1 parent a476cf3 commit f06d4ab

File tree

13 files changed

+278
-9
lines changed

13 files changed

+278
-9
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module.exports = {
22
"plugins": [
3-
"suitescript"
3+
"suitescript", "jest"
44
],
55
"extends": "airbnb-base",
66
"env": {
7+
browser: true,
78
"jest": true,
9+
"jasmine": true,
810
"suitescript/suitescript2": true,
911
"suitescript/suitescript1": true
1012
},

Examples/1.0/exampleClientScript.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require('../../index')({});
2+
3+
/**
4+
* Save Record Function
5+
* @returns {Boolean}
6+
*/
7+
function saveRecord() {
8+
try {
9+
if (nlapiGetRecordType().toString() !== 'salesorder') {
10+
return true;
11+
}
12+
const subId = parseInt(nlapiGetFieldValue('subsidiary'), 10);
13+
14+
15+
// Do not trigger for polish subsidiary and custom orders
16+
if (subId === 1) {
17+
return true;
18+
}
19+
const confirmMessage = "Do you want to auto approve this order?\nIf yes click on 'Ok' button.\nIf not please click on 'Cancel' button";
20+
// eslint-disable-next-line no-restricted-globals, no-alert
21+
if (confirm(confirmMessage)) {
22+
nlapiSetFieldValue('custbody_mycs_auto_approva_manual_ord', 'T');
23+
}
24+
25+
return true;
26+
} catch (e) {
27+
let details = '';
28+
if (e instanceof nlobjError) {
29+
details = `${e.getCode()}\n${e.getDetails()}`;
30+
} else {
31+
details = e.toString();
32+
}
33+
34+
// eslint-disable-next-line no-alert
35+
alert(`Unexpected Error occured: ${details}`);
36+
return true;
37+
}
38+
}
39+
if (typeof module !== 'undefined' && module.exports) {
40+
module.exports = {
41+
saveRecord,
42+
};
43+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const fileUnderTest = require('./exampleClientScript');
2+
3+
describe('salesorder_CL', () => {
4+
describe('saveRecord()', () => {
5+
it('should set field value for subsidiary greater than 1 after user confirmation', () => {
6+
const values = {
7+
subsidiary: random(2, 10),
8+
};
9+
const spyRecordType = spyOn(global, 'nlapiGetRecordType').and.returnValue('salesorder');
10+
const spyGetValue = spyOn(global, 'nlapiGetFieldValue').and.callFake(fieldName => values[fieldName]);
11+
const spySet = spyOn(global, 'nlapiSetFieldValue').and.callThrough();
12+
const spyAlert = spyOn(window, 'alert').and.callThrough();
13+
const spyConfirm = spyOn(window, 'confirm').and.returnValue(true);
14+
const result = fileUnderTest.saveRecord();
15+
expect(spyRecordType).toHaveBeenCalled();
16+
expect(spyGetValue).toHaveBeenCalledWith('subsidiary');
17+
expect(spyConfirm).toHaveBeenCalledWith(
18+
'Do you want to auto approve this order?\nIf yes click on \'Ok\' button.\nIf not please click on \'Cancel\' button',
19+
);
20+
expect(spyAlert).not.toHaveBeenCalled();
21+
expect(spySet).toHaveBeenCalledWith('custbody_mycs_auto_approva_manual_ord', 'T');
22+
expect(result).toEqual(true);
23+
});
24+
it('should not display confirm and retun true for subsidiary 1 ', () => {
25+
const values = {
26+
subsidiary: 1,
27+
};
28+
const spyRecordType = spyOn(global, 'nlapiGetRecordType').and.returnValue('salesorder');
29+
const spyGetValue = spyOn(global, 'nlapiGetFieldValue').and.callFake(fieldName => values[fieldName]);
30+
const spyAlert = spyOn(window, 'alert').and.callThrough();
31+
const spyConfirm = spyOn(window, 'confirm').and.callThrough();
32+
const result = fileUnderTest.saveRecord();
33+
expect(spyRecordType).toHaveBeenCalled();
34+
expect(spyGetValue).toHaveBeenCalledWith('subsidiary');
35+
expect(spyConfirm).not.toHaveBeenCalled();
36+
expect(spyAlert).not.toHaveBeenCalled();
37+
expect(result).toEqual(true);
38+
});
39+
it('should display alert in case of Javascript error', () => {
40+
const spyRecordType = spyOn(global, 'nlapiGetRecordType').and.returnValue('salesorder');
41+
spyOn(global, 'nlapiGetFieldValue').and.throwError('Test');
42+
const spyAlert = spyOn(window, 'alert').and.callThrough();
43+
const result = fileUnderTest.saveRecord();
44+
expect(spyRecordType).toHaveBeenCalled();
45+
expect(spyAlert).toHaveBeenCalledWith(expect.stringContaining('Unexpected Error occured:'));
46+
expect(result).toEqual(true);
47+
});
48+
it('should display alert in case of NetSuite error', () => {
49+
spyOn(global, 'nlapiGetFieldValue').and.callFake(() => {
50+
// eslint-disable-next-line new-cap
51+
throw new nlobjError('Test');
52+
});
53+
const spyRecordType = spyOn(global, 'nlapiGetRecordType').and.returnValue('salesorder');
54+
const spyAlert = spyOn(window, 'alert').and.callThrough();
55+
const result = fileUnderTest.saveRecord();
56+
expect(spyRecordType).toHaveBeenCalled();
57+
expect(spyAlert).toHaveBeenCalledWith(expect.stringContaining('Unexpected Error occured:'));
58+
expect(result).toEqual(true);
59+
});
60+
});
61+
});

Examples/2.0/exampleClientScript.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @NApiVersion 2.x
3+
* @NScriptType ClientScript
4+
*/
5+
6+
/**
7+
* This is an example of client script file.
8+
* In corresponding .test.js file you can find a way how to test it
9+
* of course with our ns-suitescript-mocks
10+
*/
11+
12+
// Line below is required only becasue we are in the same repository as the library,
13+
// normally define is added to globally once you import our library and follow setup instructions
14+
const { define } = require('../../index')({});
15+
16+
// eslint-disable-next-line import/no-amd
17+
define(['N/ui/message'], (msg) => {
18+
/**
19+
* Page Init function to show message after form submit
20+
*
21+
* @param {N/context} context Module
22+
* @returns {boolean}
23+
*/
24+
function pageInit(context) {
25+
// eslint-disable-next-line prefer-destructuring
26+
const currentRecord = context.currentRecord;
27+
const myMsg = msg.create({
28+
title: 'Checkbox was checked',
29+
message: 'Checkbox was checked!',
30+
type: msg.Type.CONFIRMATION,
31+
});
32+
const showMessage = currentRecord.getValue({
33+
fieldId: 'messageonpost',
34+
});
35+
if (showMessage) {
36+
myMsg.show({
37+
duration: 5000,
38+
});
39+
}
40+
41+
return true;
42+
}
43+
// required for node
44+
if (typeof module !== 'undefined' && module.exports) {
45+
module.exports = {
46+
pageInit,
47+
message: msg,
48+
};
49+
}
50+
return {
51+
pageInit,
52+
};
53+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const fileUnderTest = require('./exampleClientScript');
2+
3+
const defaultValues = {
4+
tranid: '12121',
5+
custbody_mycs_ship_method: 23,
6+
shipstatus: 'B',
7+
custbodysps_packagedetails: 'test',
8+
};
9+
const fulfillmentRecord = nlapiCreateRecord('itemfulfillment', defaultValues);
10+
const context = {
11+
currentRecord: fulfillmentRecord,
12+
};
13+
14+
describe('exampleClientScript.js', () => {
15+
describe('pageInit', () => {
16+
it('should return true', () => {
17+
const result = fileUnderTest.pageInit(context);
18+
expect(result).toBe(true);
19+
});
20+
it('should call create message method', () => {
21+
// eslint-disable-next-line prefer-destructuring
22+
const message = fileUnderTest.message;
23+
const spyMesage = spyOn(message, 'create');
24+
fileUnderTest.pageInit(context);
25+
expect(spyMesage).toHaveBeenLastCalledWith({
26+
title: 'Checkbox was checked',
27+
message: 'Checkbox was checked!',
28+
type: 'confirmation',
29+
});
30+
});
31+
it('should show message if checkbox is checked', () => {
32+
// eslint-disable-next-line prefer-destructuring
33+
const message = fileUnderTest.message;
34+
const createdMessage = message.create({
35+
title: 'Checkbox was checked',
36+
message: 'Checkbox was checked!',
37+
type: 'confirmation',
38+
});
39+
context.currentRecord.setFieldValue('messageonpost', true);
40+
const spyMesage = spyOn(message, 'create').and.returnValue(createdMessage);
41+
const spyShow = spyOn(createdMessage, 'show');
42+
fileUnderTest.pageInit(context);
43+
expect(spyMesage).toHaveBeenCalledWith({
44+
title: 'Checkbox was checked',
45+
message: 'Checkbox was checked!',
46+
type: 'confirmation',
47+
});
48+
expect(spyShow).toHaveBeenCalledWith({
49+
duration: 5000,
50+
});
51+
});
52+
it('should show not message if checkbox is not checked', () => {
53+
// eslint-disable-next-line prefer-destructuring
54+
const message = fileUnderTest.message;
55+
const createdMessage = message.create({
56+
message: 'Email with CSV was sent to your mailbox!',
57+
title: 'Email has been sent.',
58+
type: 'confirmation',
59+
});
60+
context.currentRecord.setFieldValue('messageonpost', false);
61+
const spyMesage = spyOn(message, 'create').and.returnValue(createdMessage);
62+
const spyShow = spyOn(createdMessage, 'show');
63+
fileUnderTest.pageInit(context);
64+
expect(spyMesage).toHaveBeenCalledWith({
65+
title: 'Checkbox was checked',
66+
message: 'Checkbox was checked!',
67+
type: 'confirmation',
68+
});
69+
expect(spyShow).not.toHaveBeenCalled();
70+
});
71+
});
72+
});

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ global.mockups = netsuiteMocks.nsMockups;
4343

4444
3. Write your code and use these mocks
4545

46-
`Examples will come soon`
46+
`Examples can be found in 'Examples folder in repo!'`
4747

4848
## Contribute 💻
4949

@@ -68,9 +68,9 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
6868

6969
## Roadmap 🛣
7070

71-
- add examples for jest
71+
- ~~add examples for jest~~
7272
- cleanup mycs occurences
73-
- add tests for the repo
73+
- ~~add tests for the repo~~
7474
- automate release process
7575
- add example for other test frameworks
7676

SuiteScriptMockup1.0/nsmockup-1.0.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const _ = require('lodash');
22
const moment = require('moment');
33
const record = require('../Utils/record');
44
const file = require('../N/file');
5+
const { random } = require('../Utils/utils');
56

67
const showLogs = false;
78
/**

Tests/SuiteScriptMockup1.0/nsmockup-1.0.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ describe('Testing nsmockup-1.0 library', () => {
2121
expect(fileUnderTest.nlapiGetContext()).toMatchObject(expected);
2222
});
2323
});
24+
describe('nlapiSearchRecord', () => {
25+
it('Should return an array', () => {
26+
expect(fileUnderTest.nlapiSearchRecord()).toEqual(expect.any(Array));
27+
});
28+
});
29+
describe('nlapiLookupField', () => {
30+
it('Should return a string', () => {
31+
expect(fileUnderTest.nlapiLookupField()).toEqual(expect.any(String));
32+
});
33+
});
34+
2435
describe('nlobjError', () => {
2536
it('Should create nlobjError object', () => {
2637
// eslint-disable-next-line new-cap

Tests/Utils/utils.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Tests for utils library
3+
*
4+
*/
5+
6+
const fileUnderTest = require('../../Utils/utils');
7+
8+
describe('Testing utils library', () => {
9+
describe('random', () => {
10+
it('Should return integer', () => {
11+
expect(fileUnderTest.random(1, 10)).toEqual(expect.any(Number));
12+
});
13+
});
14+
});

Utils/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
module.exports = {
3+
random: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
4+
};

0 commit comments

Comments
 (0)