-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
describe('Login Form', () => { | ||
it('can login with valid credentials', () => { | ||
cy.visit('./'); | ||
cy.get('input[name=email]').first().type('valid-name@example.com'); | ||
cy.get('input[name=password]').first().type('valid-password'); | ||
cy.get('#loginForm').submit(); | ||
cy.wait(3500); | ||
}); | ||
|
||
it('brings out an alert box with invalid credentials', () => { | ||
cy.visit('./'); | ||
cy.get('input[name=email]').first().type('invalid-name@example.com'); | ||
cy.get('input[name=password]').first().type('invalid-password'); | ||
cy.get('#loginForm').submit(); | ||
cy.on('window:alert', (alertText) => { | ||
expect(alertText).to.equal( | ||
'Login failed, please check your email or password and try again.' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
describe('Logout Button', () => { | ||
it('logs out a user', () => { | ||
cy.visit('./'); | ||
cy.get('input[name=email]').first().type('mane@suiteTeardown.noroff.no'); | ||
cy.get('input[name=password]').first().type('mane12345'); | ||
cy.get('#loginForm').submit(); | ||
|
||
cy.get('[data-auth="logout"]').click(); | ||
|
||
cy.get('#loginForm').should('be.visible'); | ||
cy.url().should('equal', 'http://localhost:55499/'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { login } from './login'; | ||
import LocalStorageMock from '../../storage/localStorage.mock'; | ||
|
||
const USER_DATA = { | ||
name: 'Student Name', | ||
email: 'studentemail@stud.noroff.no', | ||
token: '1234567890', | ||
}; | ||
|
||
const fetchsuccess = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue(USER_DATA), | ||
}); | ||
|
||
beforeAll(() => { | ||
global.localStorage = new LocalStorageMock(); | ||
}); | ||
|
||
describe('login function', () => { | ||
it('fetch and store token in browser storage', async () => { | ||
global.fetch = fetchsuccess; | ||
const data =await login({}); | ||
expect(data).toEqual(USER_DATA) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { logout } from './logout'; | ||
import LocalStorageMock from '../../storage/localStorage.mock'; | ||
|
||
const wrongtoken = 'token'; | ||
const wrongprofile = { | ||
name: 'Student Name', | ||
email: 'studentemail@stud.noroff.no', | ||
}; | ||
|
||
beforeAll(() => { | ||
global.localStorage = new LocalStorageMock(); | ||
}); | ||
|
||
describe('logout function', () => { | ||
it('this action clears the token from the browser', () => { | ||
localStorage.setItem('token', JSON.stringify(wrongtoken)); | ||
localStorage.setItem('profile', JSON.stringify(wrongprofile)); | ||
|
||
logout(); | ||
|
||
expect(localStorage.getItem('token')).toBeNull(); | ||
expect(localStorage.getItem('profile')).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createPost } from './create'; | ||
|
||
const createTestPost = { | ||
title: 'Blog post title', | ||
body: 'Hey There, welcome to my first post', | ||
media: 'https://media.istockphoto.com/id/1271163410', | ||
tags: ['post', 'climate', 'test'], | ||
}; | ||
|
||
const { title, body, media, tags } = createTestPost; | ||
|
||
const postData = { | ||
title: title, | ||
body: body, | ||
media: media, | ||
tags: tags, | ||
}; | ||
|
||
function fetchSuccess() { | ||
return Promise.resolve({ | ||
ok: true, | ||
status: 200, | ||
statusText: 'success', | ||
json: () => Promise.resolve(createTestPost), | ||
}); | ||
} | ||
|
||
describe('createPost', () => { | ||
it('create a new item on the API', async() => { | ||
global.fetch = jest.fn(() =>fetchSuccess()); | ||
const test = await createPost(postData); | ||
expect(test).toEqual(createTestPost); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default class LocalStorageMock { | ||
constructor() { | ||
this.store = {}; | ||
} | ||
|
||
clear() { | ||
this.store = {}; | ||
} | ||
|
||
getItem(key) { | ||
return this.store[key] || null; | ||
} | ||
|
||
setItem(key, value) { | ||
this.store[key] = String(value); | ||
} | ||
|
||
removeItem(key) { | ||
delete this.store[key]; | ||
} | ||
|
||
get length() { | ||
return Object.keys(this.store).length; | ||
} | ||
|
||
key(n) { | ||
const keys = Object.keys(this.store); | ||
return keys[n] || null; | ||
} | ||
} | ||
|