Skip to content

Commit

Permalink
main unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
iisa committed Jan 3, 2025
1 parent dcfdb34 commit 1b0d584
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 19 deletions.
6 changes: 0 additions & 6 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<div class="dev-tools">
<div id="options">
<button id="toggle-receipts">Toggle receipts</button>
<button id="clear-updates">Clear updates</button>

<p id="interaction-status-area" style="min-height: 20px;margin: 10px;"></p>
</div>
Expand Down Expand Up @@ -136,11 +135,6 @@

showReceipts = true;
});
document.getElementById('clear-updates').addEventListener('click', async () => {
updateNotices = [];
mgcComponent.updates = updateNotices;
await mgcComponent.updateComplete;
});
</script>
</body>
</html>
5 changes: 4 additions & 1 deletion src/monthly-giving-circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export class MonthlyGivingCircle extends LitElement {

@property({ type: Array }) updates: anUpdate[] = [];

@property({ type: String }) viewToDisplay: 'welcome' | 'receipts' = 'welcome';
@property({ type: String, reflect: true }) viewToDisplay:
| 'welcome'
| 'receipts' = 'welcome';

protected createRenderRoot() {
return this;
Expand Down Expand Up @@ -68,6 +70,7 @@ export class MonthlyGivingCircle extends LitElement {
<span slot="action">
<iaux-button-style class="link">
<button
class="close-receipts"
@click=${(event: Event) => {
const btn = event.target as HTMLButtonElement;
btn.disabled = true;
Expand Down
122 changes: 110 additions & 12 deletions test/monthly-giving-circle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,121 @@ describe('IauxMonthlyGivingCircle', () => {
html`<iaux-monthly-giving-circle></iaux-monthly-giving-circle>`
);

expect(el.viewToDisplay).to.equal('welcome');

const titleEl = el.querySelector('iaux-mgc-title');
expect(titleEl).to.not.be.null;
console.log('*****', titleEl);

expect(titleEl?.getAttribute('titlestyle')).to.equal('heartz');
expect(titleEl!.getAttribute('titlestyle')).to.equal('heart');
expect(titleEl!.children.length).to.equal(2);
expect((titleEl!.children[0] as HTMLElement).innerText).to.equal(
'Monthly Giving Circle'
);

// const welcomeEl = el.querySelector('iaux-mgc-welcome');
// expect(welcomeEl).to.not.be.null;
const welcomeEl = el.querySelector('iaux-mgc-welcome');
const joinLink = welcomeEl!.shadowRoot?.querySelector(
'a.join-mgc'
) as HTMLAnchorElement;
expect(joinLink).to.not.be.null;
expect(joinLink.href).to.equal(
'https://archive.org/donate/?amt=5&contrib_type=monthly&origin=iawww-usrsttng'
);
});

it('displays receipt button when receipts are available', async () => {
const el = await fixture<MonthlyGivingCircle>(
html`<iaux-monthly-giving-circle
.receipts=${[]}
></iaux-monthly-giving-circle>`
);
describe('Receipts', () => {
it('Displays receipts CTA when receipts are available', async () => {
const el = await fixture<MonthlyGivingCircle>(
html`<iaux-monthly-giving-circle
.receipts=${[
{
amount: 9999.99,
date: '2020-09-01',
donor: 'John Doe',
paymentMethod: 'Credit Card',
status: 'Completed',
id: 'foo-id-1',
},
]}
></iaux-monthly-giving-circle>`
);

const titleEl = el.querySelector('iaux-mgc-title');
const receiptsButton = titleEl!.querySelector('button');
expect(receiptsButton).to.exist;
expect(receiptsButton!.innerText).to.equal(
'View recent donation history'
);

el.receipts = [];
await el.updateComplete;

const newTitleEl = el.querySelector('iaux-mgc-title');
expect(newTitleEl).to.exist;

const newReceiptsButton = newTitleEl!.querySelector('button');
expect(newReceiptsButton).to.not.exist;
});

it('Display receipts table when receipts CTA is clicked', async () => {
const el = await fixture<MonthlyGivingCircle>(
html`<iaux-monthly-giving-circle
.receipts=${[
{
amount: 9999.99,
date: '2020-09-01',
donor: 'John Doe',
paymentMethod: 'Credit Card',
status: 'Completed',
id: 'foo-id-1',
},
]}
></iaux-monthly-giving-circle>`
);

const titleEl = el.querySelector('iaux-mgc-title');
const receiptsButton = titleEl!.querySelector('button');
expect(receiptsButton).to.exist;
receiptsButton!.click();

await el.updateComplete;

expect(el.viewToDisplay).to.equal('receipts');

const welcomeEl = el.querySelector('iaux-mgc-welcome');
expect(welcomeEl).to.not.exist;

// shows proper title
const titleEl2 = el.querySelector('iaux-mgc-title');
expect(titleEl2).to.exist;
expect(titleEl2!.getAttribute('titlestyle')).to.equal('default');
const titleValueEl = titleEl2?.querySelector(
'span[slot="title"]'
) as HTMLSpanElement;
expect(titleValueEl).to.exist;
expect(titleValueEl.innerText).to.equal('Recent donations');

// shows back button
const backButton = titleEl2?.querySelector(
'button.close-receipts'
) as HTMLButtonElement;
expect(backButton!.innerText).to.equal('Back to account settings');

// shows receipts element
const receiptsEl = el.querySelector('iaux-mgc-receipts');
expect(receiptsEl).to.exist;

// goes back to welcome page if back button is clicked
backButton!.click();
await el.updateComplete;

expect(el.viewToDisplay).to.equal('welcome');

expect(el.querySelector('iaux-mgc-welcome')).to.not.be.null;
const titleEl3 = el.querySelector('iaux-mgc-title');
expect(titleEl3).to.exist;
expect(titleEl3!.getAttribute('titlestyle')).to.equal('heart');
const welcomeTitle = titleEl3?.querySelector(
'span[slot="title"]'
) as HTMLSpanElement;
expect(welcomeTitle.innerText).to.equal('Monthly Giving Circle');
});
});
});

0 comments on commit 1b0d584

Please sign in to comment.