Skip to content

Commit b58d587

Browse files
committed
Add htmlStatement() and relevant UTs with 100% coverage still
1 parent 9eb6156 commit b58d587

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

index.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import createStatementData from './createStatementData';
22

3-
function statement(invoice, plays) {
3+
export function statement(invoice, plays) {
44
return renderPlainText(createStatementData(invoice, plays));
55
}
66

@@ -16,12 +16,28 @@ function renderPlainText(data) {
1616
return result;
1717
}
1818

19+
export function htmlStatement(invoice, plays) {
20+
return renderHtml(createStatementData(invoice, plays));
21+
}
22+
23+
function renderHtml(data) {
24+
let result = `<h1>Statement for ${data.customer}</h1>\n`;
25+
result += "<table>\n";
26+
result += "<tr><th>play</th><th>seats</th><th>cost</th></tr>\n";
27+
for (let perf of data.performances) {
28+
result += ` <tr><td>${perf.play.name}</td><td>${perf.audience}</td>`;
29+
result += `<td>${usd(perf.amount)}</td></tr>\n`;
30+
}
31+
result += "</table>\n";
32+
result += `<p>Amount owed is <em>${usd(data.totalAmount)}</em></p>\n`;
33+
result += `<p>You earned <em>${data.totalVolumeCredits}</em> credits</p>\n`;
34+
return result;
35+
}
36+
1937
function usd(aNumber) {
2038
return new Intl.NumberFormat("en-US", {
2139
style: "currency",
2240
currency: "USD",
2341
minimumFractionDigits: 2
2442
}).format(aNumber/100);
2543
}
26-
27-
export default statement;

index.test.js

+92-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import statement from './index';
1+
import { statement, htmlStatement } from './index';
22

33
import plays from './plays.json';
44
import invoices from './invoices.json';
@@ -136,3 +136,94 @@ describe('Tests of statement() with invalid play type', () => {
136136
expect(() => statement(playUnknown, mockPlays)).toThrow(Error);
137137
});
138138
});
139+
140+
describe('Tests of htmlStatement() with full set of test data', () => {
141+
const sttResult = htmlStatement(invoices[0], plays);
142+
console.log(sttResult)
143+
144+
test('verify the full output', () => {
145+
expect(sttResult).toBeDefined();
146+
expect(sttResult).toMatch(/Statement for BigCo/);
147+
expect(sttResult).toMatch(/<td>Hamlet<\/td><td>55<\/td><td>\$650.00<\/td>/);
148+
expect(sttResult).toMatch(/<td>As You Like It<\/td><td>35<\/td><td>\$580.00<\/td>/);
149+
expect(sttResult).toMatch(/<td>Othello<\/td><td>40<\/td><td>\$500.00<\/td>/);
150+
expect(sttResult).toMatch(/Amount owed is <em>\$1,730.00<\/em>/);
151+
expect(sttResult).toMatch(/You earned <em>47<\/em> credits/);
152+
});
153+
});
154+
155+
describe('Tests of htmlStatement() with only one comedy play', () => {
156+
test('verify comedy with 1 audience', () => {
157+
const playComedy = {
158+
"customer": "BigCo",
159+
"performances": [
160+
{
161+
"playID": "as-like",
162+
"audience": 1
163+
}
164+
]
165+
};
166+
167+
const sttResult = htmlStatement(playComedy, plays);
168+
expect(sttResult).toMatch(/Statement for BigCo/);
169+
expect(sttResult).toMatch(/<td>As You Like It<\/td><td>1<\/td><td>\$303.00<\/td>/);
170+
expect(sttResult).toMatch(/Amount owed is <em>\$303.00<\/em>/);
171+
expect(sttResult).toMatch(/You earned <em>0<\/em> credits/);
172+
});
173+
174+
test('verify comedy with 5 audience', () => {
175+
const playComedy = {
176+
"customer": "BigCo",
177+
"performances": [
178+
{
179+
"playID": "as-like",
180+
"audience": 5
181+
}
182+
]
183+
};
184+
185+
const sttResult = htmlStatement(playComedy, plays);
186+
expect(sttResult).toMatch(/Statement for BigCo/);
187+
expect(sttResult).toMatch(/<td>As You Like It<\/td><td>5<\/td><td>\$315.00<\/td>/);
188+
expect(sttResult).toMatch(/Amount owed is <em>\$315.00<\/em>/);
189+
expect(sttResult).toMatch(/You earned <em>1<\/em> credits/);
190+
});
191+
});
192+
193+
describe('Tests of htmlStatement() with only one tragedy play', () => {
194+
test('verify tragedy with 1 audience', () => {
195+
const playTragedy = {
196+
"customer": "BigCo",
197+
"performances": [
198+
{
199+
"playID": "othello",
200+
"audience": 1
201+
}
202+
]
203+
};
204+
205+
const sttResult = htmlStatement(playTragedy, plays);
206+
expect(sttResult).toMatch(/Statement for BigCo/);
207+
expect(sttResult).toMatch(/<td>Othello<\/td><td>1<\/td><td>\$400.00<\/td>/);
208+
expect(sttResult).toMatch(/Amount owed is <em>\$400.00<\/em>/);
209+
expect(sttResult).toMatch(/You earned <em>0<\/em> credits/);
210+
});
211+
212+
test('verify tragedy with 5 audience', () => {
213+
const playTragedy = {
214+
"customer": "BigCo",
215+
"performances": [
216+
{
217+
"playID": "othello",
218+
"audience": 5
219+
}
220+
]
221+
};
222+
223+
const sttResult = htmlStatement(playTragedy, plays);
224+
expect(sttResult).toMatch(/Statement for BigCo/);
225+
expect(sttResult).toMatch(/<td>Othello<\/td><td>5<\/td><td>\$400.00<\/td>/);
226+
expect(sttResult).toMatch(/Amount owed is <em>\$400.00<\/em>/);
227+
expect(sttResult).toMatch(/You earned <em>0<\/em> credits/);
228+
});
229+
});

0 commit comments

Comments
 (0)