Skip to content

Commit

Permalink
added localstorage.mock.js
Browse files Browse the repository at this point in the history
  • Loading branch information
EF-Pami committed Jan 26, 2024
1 parent 45ca825 commit 3893fcd
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cypress/e2e/login.cy.js
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.'
);
});
});
});
13 changes: 13 additions & 0 deletions cypress/e2e/logout.cy.js
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/');
});
});
25 changes: 25 additions & 0 deletions src/js/api/auth/login.test.js
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)
});
});
24 changes: 24 additions & 0 deletions src/js/api/auth/logout.test.js
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();
});
});
34 changes: 34 additions & 0 deletions src/js/api/posts/create.test.js
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);
});
});
31 changes: 31 additions & 0 deletions src/js/storage/localStorage.mock.js
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;
}
}

0 comments on commit 3893fcd

Please sign in to comment.