Skip to content

Stephen's frontend-api-challenge #125

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
95a3455
project set up
stephenfletchtek Sep 3, 2022
5b7894c
first test passed
stephenfletchtek Sep 3, 2022
8dbf2b0
implement and test drive PeepsModel
stephenfletchtek Sep 3, 2022
9f02184
jest environment jsdom added and peepsView skeleton created
stephenfletchtek Sep 3, 2022
5bddb50
test drive ChitterApi loadPeeps method
stephenfletchtek Sep 3, 2022
0716c8b
display peeps tested
stephenfletchtek Sep 3, 2022
f1d5b51
displayClear method test driven
stephenfletchtek Sep 3, 2022
a76ae35
test drive displayFromApi method
stephenfletchtek Sep 3, 2022
bfa7b95
test drive elapsed days and display handle and days about peep
stephenfletchtek Sep 3, 2022
cd8c275
test drive api createUser method
stephenfletchtek Sep 3, 2022
95570c4
submit button works but not capturing text input
stephenfletchtek Sep 4, 2022
cd97e3d
passes with text element values assigned in class
stephenfletchtek Sep 4, 2022
a9cf1a7
cannot get input values from jest test into class
stephenfletchtek Sep 4, 2022
b85eebc
add user test passes with nasty workaround
stephenfletchtek Sep 4, 2022
7ae6e52
test passed at last!
stephenfletchtek Sep 4, 2022
eb87919
test passed
stephenfletchtek Sep 4, 2022
05f5c97
api.loadSession methdod test driven
stephenfletchtek Sep 4, 2022
b8d59f1
log in works with fake response - need to implement response
stephenfletchtek Sep 4, 2022
515c5f3
test drive login success
stephenfletchtek Sep 4, 2022
f90930f
fixed run build issue
stephenfletchtek Sep 4, 2022
35b35c8
re set
stephenfletchtek Sep 4, 2022
86600d3
fix session api typo - can now log in!
stephenfletchtek Sep 4, 2022
ef70ebb
basic login
stephenfletchtek Sep 4, 2022
7e183a5
api postPeep test driven
stephenfletchtek Sep 4, 2022
65b1777
sign up, sign in and post a peep but layout is a mess
stephenfletchtek Sep 4, 2022
9c6bfb8
moved text input and submit to post a peep to the top of the page
stephenfletchtek Sep 4, 2022
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
bundle.js
user.json
48 changes: 48 additions & 0 deletions chitterApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class ChitterApi {

loadPeeps(callback) {
const callbackFn = callback || (() => { })

Choose a reason for hiding this comment

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

You reference this a lot - not sure why you have it but you could make it a line in the constructor eg. this.callbackFn() instead of writing it every time

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

createUser(user, callback) {
const callbackFn = callback || (() => { })
fetch('https://chitter-backend-api-v2.herokuapp.com/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(user)
})
.then(response => response.json())
.then(data => callbackFn(data));
}

loadSession(session, callback) {
const callbackFn = callback || (() => { })
fetch('https://chitter-backend-api-v2.herokuapp.com/sessions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(session)
})
.then(response => response.json())
.then(data => callbackFn(data));
}

postPeep(token, peep, callback) {
const callbackFn = callback || (() => { })
fetch('https://chitter-backend-api-v2.herokuapp.com/peeps', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Token token=' + token
},
body: JSON.stringify(peep)
})
.then(response => response.json())
.then(data => callbackFn(data));
}

}

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

describe('ChitterApi class', () => {
beforeEach(() => {
api = new ChitterApi;
})

it('views all peeps', () => {
expect.assertions(1);
fetch.mockResponseOnce(JSON.stringify({ body: 'my first peep' }))
api.loadPeeps((data) => {
expect(data.body).toBe('my first peep');
})
})

it('creates a user', () => {
expect.assertions(2);
fetch.mockResponseOnce(JSON.stringify({ id: 1, handle: "user" }));
api.createUser({ user: { handle: "user", password: "secret" } }, (response) => {
expect(response.id).toEqual(1);
expect(response.handle).toEqual('user');
})
})

it('creates a session', () => {
expect.assertions(2);
fetch.mockResponseOnce(JSON.stringify({ user_id: 1, session_key: "stuff" }));
const session = { session: { handle: "test", password: "test" } }
api.loadSession(session, (response) => {
expect(response.user_id).toEqual(1);
expect(response.session_key).toEqual("stuff");
})
})

it('creates a peep', () => {
expect.assertions(1);
fetch.mockResponseOnce(JSON.stringify({ body: 'quack!' }));
api.postPeep('fake', 'whassup?', (response) => {
expect(response.body).toEqual('quack!');
})
})

})
26 changes: 26 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!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">
<script type="text/javascript" src="bundle.js" defer></script>

<title>Chitter</title>
</head>

<body>
<h1>Chitter</h1>
<div id="main-container">
<div id="user">
<button id="post">Post Peep</button>
<button id="sign-up">Sign Up</button>
<button id="sign-in">Sign In</button>
</div>
<div id="post-peep"></div>

</div>
</body>

</html>
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const PeepsModel = require('./peepsModel')
const ChitterApi = require('./chitterApi');
const PeepsView = require('./peepsView');

const model = new PeepsModel;
const api = new ChitterApi;
const peepsView = new PeepsView(model, api);

console.log('Chitter is running');

peepsView.displayFromApi();
Loading