Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm start & sleep 5 && npm test
- name: Upload tests report(cypress mochaawesome merged HTML report)
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: report
path: reports
28 changes: 27 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// const { createElement } = require('react');

const food = {
Drink: {
Wine: {},
Expand All @@ -21,7 +23,31 @@ const food = {
const tree = document.querySelector('#tree');

function createTree(element, data) {
// WRITE YOUR CODE HERE
if (!element) {
throw new Error('Tree container not found');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good — this throws a clear Error when the container element is missing, matching the checklist requirement to fail loudly instead of silently returning.

}

if (!data || typeof data !== 'object' || Object.keys(data).length === 0) {
return;
Comment on lines +30 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guard here prevents calling Object.keys on null/undefined and thus avoids runtime errors. Returning early for empty objects is acceptable, but be sure this behavior matches the tests' expectations (alternatively you could treat empty data as a no-op explicitly).

}

const newUl = document.createElement('ul');

for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
const newLi = document.createElement('li');

newLi.textContent = key;

createTree(newLi, data[key]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recursive tree-building looks correct, but the module still triggers a top-level side-effect on import (the file calls createTree(tree, food) elsewhere). Remove that import-time call — instead export createTree for tests (or move any demo call into a DOMContentLoaded handler in the HTML). Leaving the top-level call will cause tests to fail when #tree is absent.


newUl.append(newLi);
}
}

if (newUl.children.length > 0) {
element.append(newUl);
}
}

createTree(tree, food);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This top-level invocation runs during module load and can cause runtime issues in tests if #tree doesn't exist. Remove or guard this call — either export createTree and let tests/demo call it, or wrap it in DOMContentLoaded and/or throw when tree is missing. Running DOM-manipulation at import time is brittle for test environments.

Loading