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

Partly completed Chitter #133

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implemented and tested postPeep method in Client class
  • Loading branch information
kateusacova committed Oct 29, 2022
commit 2d8b0d437e4ba1a5badf0d0204eae3a60629db74
21 changes: 21 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -46,6 +46,27 @@ class Client {
console.log(error); // change to Display Error method?
})
}

postPeep = (userId, sessionKey, peepBody, displayResult) => {
fetch('https://chitter-backend-api-v2.herokuapp.com/peeps', {
method: 'POST',
headers: {
'Authorization': 'Token token=' + sessionKey,
'Content-Type': 'application/json'
},
body: JSON.stringify({peep: {user_id:userId, body:peepBody}})
})
.then((response) => response.json())
.then((output) => {
console.log(output); // remove later maybe
displayResult(output);
})
.catch((error) => {
console.log(error); // change to Display Error method?
})
}


}

module.exports = Client;
21 changes: 19 additions & 2 deletions client.test.js
Original file line number Diff line number Diff line change
@@ -55,10 +55,27 @@ describe('Client', () => {

client.signinUser(inputUsername, inputPassword, () => {
expect(fetch.mock.calls.length).toEqual(1);
console.log(fetch.mock.calls[0]);
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify({session: {handle:inputUsername, password:inputPassword}}));
done();
});

});

it('Posts peep to the server', (done) => {
const client = new Client;

fetch.mockResponseOnce(JSON.stringify({
id:1,
body:"Test peep"
}));

let userId = 1;
let sessionKey = 'random';
let peepBody = 'Test peep';

client.postPeep(userId, sessionKey, peepBody, () => {
expect(fetch.mock.calls.length).toEqual(1);
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify({peep: {user_id:userId, body:peepBody}}));
done();
});
});
});