Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ladan API challenge #129

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bundle.js
10 changes: 10 additions & 0 deletions chitterApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ChitterApi {
loadPeeps(callback) {
fetch('https://chitter-backend-api-v2.herokuapp.com/')

Choose a reason for hiding this comment

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

You can use one url

fetch('https://chitter-backend-api-v2.herokuapp.com/peeps')
.then(response => response.json())
.then(peeps => callback(peeps));
};
};

module.exports = ChitterApi;
22 changes: 22 additions & 0 deletions chitterApi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const ChitterApi = require('./chitterApi');

require('jest-fetch-mock').enableMocks()

describe(ChitterApi,() => {
it('returns peeps in reverse order',() => {
const api = new ChitterApi();
const peepData = {
"id":2000,
"body":"test peep",
"created_at":"2022-09-04T12:24:57.177Z",
"updated_at":"2022-09-04T15:24:57.177Z",
"user":{"id":1,"handle":"you"},
"likes":[]}

fetch.mockResponseOnce(JSON.stringify([peepData,peepData]));

api.loadPeeps((peeps) => {
expect(peeps).toEqual([peepData, peepData])
});
});
});
Empty file added chitterModel.js
Empty file.
Empty file added chitterModel.test.js
Empty file.
19 changes: 19 additions & 0 deletions chitterView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ChitterView {
constructor(api) {
this.mainContainerEl = document.querySelector('#main-container');
this.api = api
}

displayPeeps() {
this.api.loadPeeps((peeps) => {
peeps.forEach((peep) => {
let div = document.createElement("div");
div.className = "peep";
div.innerText = peep.body;
this.mainContainerEl.append(div);
});
});
}
}

module.exports = ChitterView;
28 changes: 28 additions & 0 deletions chitterView.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @jest-environment jsdom
*/

const fs = require('fs');

const ChitterView = require('./chitterView');

describe(ChitterView,() => {
beforeEach(() => {
document.body.innerHTML = fs.readFileSync('./index.html');
});
it('returns the list of peeps ',() => {
const peepData = {
"id":2000,
"body":"test peep",
"created_at":"2022-09-04T12:24:57.177Z",
"updated_at":"2022-09-04T15:24:57.177Z",
"user":{"id":1,"handle":"you"},
"likes":[]}
this.mockApi = {
loadPeeps: (callback) => {callback([peepData, peepData])}
}
const view = new ChitterView(this.mockApi);
view.displayPeeps();
expect(document.querySelectorAll('div.peep').length).toEqual(2);
});
});
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>My Chitter web page</title>
</head>
<body>
<p>Chitter </p>
<div id="main-container"></div>
<!-- Load the bundle.js file on the page -->

<script type="text/javascript" src="bundle.js"></script>

</body>
</html>
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const ChitterApi = require('./chitterApi');
const ChitterView = require('./chitterView');

const api = new ChitterApi();
const view = new ChitterView(api);

view.displayPeeps();
Loading