Skip to content

Commit 1126bbf

Browse files
Add files via upload
1 parent dd9600d commit 1126bbf

File tree

7 files changed

+1151
-1
lines changed

7 files changed

+1151
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# Unit-Testing-and-Integration-Testing-2
1+
# Unit-Testing-and-Integration-Testing
2+
Unit Testing and Integration Testing

azure-pipelines.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Node.js
2+
# Build a general Node.js project with npm.
3+
# Add steps that analyze code, save build artifacts, deploy, and more:
4+
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript
5+
6+
trigger:
7+
- master
8+
9+
pool:
10+
vmImage: 'ubuntu-latest'
11+
12+
steps:
13+
- task: NodeTool@0
14+
inputs:
15+
versionSpec: '10.x'
16+
displayName: 'Install Node.js'
17+
18+
- script: |
19+
npm install
20+
npm test
21+
displayName: 'npm install and test'

index.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>KEANet</title>
5+
</head>
6+
<body>
7+
<h1>KEANet</h1>
8+
<div>
9+
<input type="checkbox" id="chkInternetConnection">
10+
<label for="chkInternetConnection" id="internetConnection">Internet connection</label>
11+
<br>
12+
<label for="txtPhoneLines">Phone lines</label>
13+
<input type="number" id="txtPhoneLines" value="0">
14+
<br>
15+
<label for="cmbCellPhones">Cell phones:</label>
16+
<br>
17+
<table>
18+
<tr>
19+
<td>
20+
<select id="cmbCellPhones" size="5">
21+
<option value="moto" selected="selected">Motorola G99</option>
22+
<option value="iphone">iPhone 99</option>
23+
<option value="samsung">Samsung Galaxy 99</option>
24+
<option value="sony">Sony Xperia 99</option>
25+
<option value="huawei">Huawei 99</option>
26+
</select>
27+
</td>
28+
<td>
29+
<input type="button" value="&gt;" id="rightBtn">
30+
<br>
31+
<input type="button" value="&lt;" id="leftBtn">
32+
</td>
33+
<td>
34+
<select id="txtChosenCellPhones" size="5" style="width: 150px">
35+
</select>
36+
</td>
37+
</tr>
38+
</table>
39+
<p id="price">Total price: 0 DKK</p>
40+
<input id="buyBtn" type="submit" value="Buy">
41+
</div>
42+
</body>
43+
<footer>
44+
<script type="text/javascript" src="index.js"></script>
45+
</footer>
46+
</html>

index.js

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
const internetConnectionPrice = 200;
2+
const phoneLinePrice = 150;
3+
const motorolaPrice = 800;
4+
const iPhonePrice = 6000;
5+
const samsungPrice = 1000;
6+
const sonyPrice = 900;
7+
const huaweiPrice = 900;
8+
let totalPrice = 0;
9+
let isInternetConnection = false;
10+
let phoneLines = 0;
11+
let selectedCellPhones = [];
12+
let receiptNames = ["Internet connection", "Phone lines", "Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
13+
let receiptQuantity = [0, 0, 0, 0, 0, 0, 0]; // frequency array
14+
let sel;
15+
let selectedLeftItemName;
16+
let selectedRightItemName;
17+
let indexChosenCellPhones;
18+
let opt;
19+
20+
function getTotalPrice () {
21+
document.getElementById("price").innerHTML = `Total price: ${totalPrice} DKK`;
22+
}
23+
24+
function getSelectedLeftItem() { // get the name of the selected option from the Left side, return 0 if no option is selected
25+
sel = document.getElementById("cmbCellPhones");
26+
console.log("sel", sel);
27+
if (sel.options[sel.selectedIndex]) { //if an option is selected
28+
selectedLeftItemName = sel.options[sel.selectedIndex].text;
29+
console.log("selectedLeftItemName", selectedLeftItemName);
30+
return 1;
31+
} else {
32+
return 0;
33+
}
34+
}
35+
36+
function getSelectedRightItem() { // get the name of the selected option from the Right side, return 0 if no option is selected
37+
sel = document.getElementById("txtChosenCellPhones");
38+
console.log("sel", sel);
39+
if (sel.options[sel.selectedIndex]) { //if an option is selected
40+
selectedRightItemName = sel.options[sel.selectedIndex].text;
41+
indexChosenCellPhones = sel.selectedIndex;
42+
console.log("selectedRightItemName", selectedRightItemName);
43+
console.log("indexChosenCellPhones", indexChosenCellPhones);
44+
return 1;
45+
} else {
46+
return 0;
47+
}
48+
}
49+
50+
function alertMessage() {
51+
let message = '';
52+
53+
if(receiptQuantity[1] === 1) {
54+
receiptNames[1] = "Phone line";
55+
} else if(receiptQuantity[1] > 1) {
56+
receiptNames[1] = "Phone lines"
57+
}
58+
59+
for (let i = 0; i < receiptQuantity.length; i++){
60+
if (receiptQuantity[i] > 0) {
61+
message = message + ' \u2022 ' + receiptQuantity[i] + 'x ' + receiptNames[i] + '\n';
62+
}
63+
}
64+
message = message + 'Total price: ' + totalPrice + '\n';
65+
if (totalPrice !== 0) {
66+
return 'You have selected: \n' + message;
67+
} else {
68+
return "Nothing is selected! Please select something";
69+
}
70+
}
71+
72+
document.getElementById("chkInternetConnection").addEventListener("click", () => {
73+
if (isInternetConnection) {
74+
isInternetConnection = false;
75+
totalPrice = totalPrice - internetConnectionPrice;
76+
receiptQuantity[0] = 0; // isInternetConnection is set to 0 for the receipt
77+
} else {
78+
isInternetConnection = true;
79+
totalPrice = totalPrice + internetConnectionPrice;
80+
receiptQuantity[0] = 1; // isInternetConnection is set to 1 for the receipt
81+
}
82+
console.log("isInternetConnection: ", isInternetConnection);
83+
console.log("totalPrice: ", totalPrice);
84+
getTotalPrice();
85+
});
86+
87+
document.getElementById("txtPhoneLines").addEventListener("input", (e) => {
88+
// reset the total price to 0
89+
totalPrice = totalPrice - phoneLines * phoneLinePrice;
90+
// regex for digits between 0 and 8
91+
const numbers = /^[0-9]+$/;
92+
// if the input field is less than 0, is not a number or the length is higher than 1, then the input field is reset to 0
93+
if(e.target.value < 0 || !e.target.value.match(numbers)) {
94+
e.target.value = 0;
95+
} else if (e.target.value > 8 || e.target.value.toString().length > 1){
96+
e.target.value = 8;
97+
}
98+
console.log("Phone lines: ", e.target.value);
99+
phoneLines = e.target.value;
100+
receiptQuantity[1] = Number(phoneLines); // set the phoneLines quantity for the receipt
101+
// calculate the new total price
102+
totalPrice = totalPrice + phoneLines * phoneLinePrice;
103+
console.log("totalPrice: ", totalPrice);
104+
getTotalPrice();
105+
});
106+
107+
document.getElementById("rightBtn").addEventListener("click", ()=> {
108+
console.log(" ------------ rightBtn ------------");
109+
if (getSelectedLeftItem() === 0) { // if no element is selected
110+
return 0;
111+
} else {
112+
console.log("selectedCellPhones-: ", selectedCellPhones );
113+
if (selectedLeftItemName !== undefined) {
114+
selectedCellPhones.push(selectedLeftItemName);
115+
console.log("selectedCellPhones+: ", selectedCellPhones );
116+
}
117+
118+
let select = document.getElementById('txtChosenCellPhones');
119+
console.log("select.length: ", select.length );
120+
121+
select.textContent = ''; // Delete the content of the Select element from HTML (all the "Option" children)
122+
console.log("selectedCellPhones.length: ", selectedCellPhones.length );
123+
124+
for (let i = 0; i < selectedCellPhones.length; i++) { // insert every element of the Array as an Option tag in HTML
125+
opt = document.createElement('option');
126+
opt.value = selectedCellPhones[i];
127+
opt.innerHTML = selectedCellPhones[i];
128+
opt.selected = true;
129+
select.appendChild(opt);
130+
}
131+
}
132+
133+
if(selectedLeftItemName === "Motorola G99") {
134+
totalPrice = totalPrice + motorolaPrice;
135+
receiptQuantity[2]++;
136+
} else if(selectedLeftItemName === "iPhone 99") {
137+
totalPrice = totalPrice + iPhonePrice;
138+
receiptQuantity[3]++;
139+
} else if(selectedLeftItemName === "Samsung Galaxy 99") {
140+
totalPrice = totalPrice + samsungPrice;
141+
receiptQuantity[4]++;
142+
} else if(selectedLeftItemName === "Sony Xperia 99") {
143+
totalPrice = totalPrice + sonyPrice;
144+
receiptQuantity[5]++;
145+
} else if(selectedLeftItemName === "Huawei 99") {
146+
totalPrice = totalPrice + huaweiPrice;
147+
receiptQuantity[6]++;
148+
}
149+
getTotalPrice();
150+
console.log("totalPrice: ", totalPrice);
151+
152+
153+
});
154+
155+
document.getElementById("leftBtn").addEventListener("click", ()=> {
156+
console.log(" ------------ leftBtn ------------");
157+
if (getSelectedRightItem() === 0) { // if no element is selected
158+
return 0;
159+
} else {
160+
console.log("selectedCellPhones: ", selectedCellPhones );
161+
console.log("indexChosenCellPhones: ", indexChosenCellPhones );
162+
if (indexChosenCellPhones > -1) {
163+
selectedCellPhones.splice(indexChosenCellPhones, 1); // delete the selected element
164+
}
165+
console.log("selectedCellPhones-: ", selectedCellPhones );
166+
console.log("indexChosenCellPhones: ", indexChosenCellPhones );
167+
168+
let select = document.getElementById('txtChosenCellPhones');
169+
170+
select.textContent = ''; // Delete the content of the Select element from HTML (all the "Option" children)
171+
for (let i = 0; i < selectedCellPhones.length; i++){ // insert the Option elements in HTML
172+
opt = document.createElement('option');
173+
opt.value = selectedCellPhones[i];
174+
opt.innerHTML = selectedCellPhones[i];
175+
opt.selected = true;
176+
select.appendChild(opt);
177+
}
178+
}
179+
180+
console.log("sel.value: ", sel.value);
181+
if(totalPrice > 0) {
182+
if (selectedRightItemName === "Motorola G99") {
183+
totalPrice = totalPrice - motorolaPrice;
184+
receiptQuantity[2]--;
185+
} else if (selectedRightItemName === "iPhone 99") {
186+
totalPrice = totalPrice - iPhonePrice;
187+
receiptQuantity[3]--;
188+
} else if (selectedRightItemName === "Samsung Galaxy 99") {
189+
totalPrice = totalPrice - samsungPrice;
190+
receiptQuantity[4]--;
191+
} else if (selectedRightItemName === "Sony Xperia 99") {
192+
totalPrice = totalPrice - sonyPrice;
193+
receiptQuantity[5]--;
194+
} else if (selectedRightItemName === "Huawei 99") {
195+
totalPrice = totalPrice - huaweiPrice;
196+
receiptQuantity[6]--;
197+
}
198+
}
199+
200+
getTotalPrice();
201+
console.log("totalPrice: ", totalPrice);
202+
});
203+
204+
document.getElementById("buyBtn").addEventListener("click", () => {
205+
alert(alertMessage());
206+
});

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "unittests",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "mocha",
7+
"coverage": "nyc --reporter html --reporter text npm test"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"description": "Unit Testing and Integration Testing",
13+
"devDependencies": {
14+
"chai": "^4.2.0",
15+
"mocha": "^7.1.1",
16+
"nyc": "^15.0.0"
17+
},
18+
"dependencies": {}
19+
}

0 commit comments

Comments
 (0)