-
Notifications
You must be signed in to change notification settings - Fork 5
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
9 changed files
with
191 additions
and
13 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
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,50 @@ | ||
const itActsAsFavoriteRestoModel = (favRestoIdb) => { | ||
it('should return the resto that has been added', async () => { | ||
favRestoIdb.putResto({ id: 1 }); | ||
favRestoIdb.putResto({ id: 2 }); | ||
|
||
expect(await favRestoIdb.getResto(1)).toEqual({ id: 1 }); | ||
expect(await favRestoIdb.getResto(2)).toEqual({ id: 2 }); | ||
expect(await favRestoIdb.getResto(3)).toEqual(undefined); | ||
}); | ||
|
||
it('should refuse a resto from being added if it does not have the correct property', async () => { | ||
favRestoIdb.putResto({ aProperty: 'property' }); | ||
|
||
expect(await favRestoIdb.getAllResto()).toEqual([]); | ||
}); | ||
|
||
it('can return all of the resto that have been added', async () => { | ||
favRestoIdb.putResto({ id: 1 }); | ||
favRestoIdb.putResto({ id: 2 }); | ||
|
||
expect(await favRestoIdb.getAllResto()).toEqual([{ id: 1 }, { id: 2 }]); | ||
}); | ||
|
||
it('should remove favorite resto', async () => { | ||
favRestoIdb.putResto({ id: 1 }); | ||
favRestoIdb.putResto({ id: 2 }); | ||
favRestoIdb.putResto({ id: 3 }); | ||
|
||
await favRestoIdb.deleteResto(1); | ||
|
||
expect(await favRestoIdb.getAllResto()).toEqual([{ id: 2 }, { id: 3 }]); | ||
}); | ||
|
||
it('should handle request to remove a resto even though the resto has not been added', async () => { | ||
favRestoIdb.putResto({ id: 1 }); | ||
favRestoIdb.putResto({ id: 2 }); | ||
favRestoIdb.putResto({ id: 3 }); | ||
|
||
await favRestoIdb.deleteResto(4); | ||
|
||
expect(await favRestoIdb.getAllResto()).toEqual([ | ||
{ id: 1 }, | ||
{ id: 2 }, | ||
{ id: 3 }, | ||
]); | ||
}); | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export { itActsAsFavoriteRestoModel }; |
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,44 @@ | ||
/* eslint-disable no-prototype-builtins */ | ||
/* eslint-disable consistent-return */ | ||
import { itActsAsFavoriteRestoModel } from './contract/favRestoContract'; | ||
|
||
let favoriteRestos = []; | ||
|
||
const FavoriteRestoArray = { | ||
getResto(id) { | ||
if (!id) { | ||
return; | ||
} | ||
|
||
return favoriteRestos.find((restaurant) => restaurant.id === id); | ||
}, | ||
|
||
getAllResto() { | ||
return favoriteRestos; | ||
}, | ||
|
||
putResto(resto) { | ||
if (!resto.hasOwnProperty('id')) { | ||
return; | ||
} | ||
|
||
if (this.getResto(resto.id)) { | ||
return; | ||
} | ||
|
||
favoriteRestos.push(resto); | ||
}, | ||
|
||
deleteResto(id) { | ||
// cara boros menghapus restaurant dengan meng-copy restaurant yang ada | ||
// kecuali restaurant dengan id === id | ||
favoriteRestos = favoriteRestos.filter((resto) => resto.id !== id); | ||
}, | ||
}; | ||
|
||
describe('Favorite resto array contract test', () => { | ||
// eslint-disable-next-line no-return-assign | ||
afterEach(() => (favoriteRestos = [])); | ||
|
||
itActsAsFavoriteRestoModel(FavoriteRestoArray); | ||
}); |
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,12 @@ | ||
import { itActsAsFavoriteRestoModel } from './contract/favRestoContract'; | ||
import FavRestoIdb from '../src/scripts/data/resto-idb'; | ||
|
||
describe('Favorite Movie Idb Contract Test Implementation', () => { | ||
afterEach(async () => { | ||
(await FavRestoIdb.getAllResto()).forEach(async (resto) => { | ||
await FavRestoIdb.deleteResto(resto.id); | ||
}); | ||
}); | ||
|
||
itActsAsFavoriteRestoModel(FavRestoIdb); | ||
}); |
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,15 @@ | ||
import LikeButtonPresenter from '../../src/scripts/utils/like-button-presenter'; | ||
import FavRestoIdb from '../../src/scripts/data/resto-idb'; | ||
|
||
const createLikeButtonPresenterWithResto = async (restaurant) => { | ||
await LikeButtonPresenter.init({ | ||
likeButtonContainer: document.querySelector('#likeButtonContainer'), | ||
favRestoIdb: FavRestoIdb, | ||
data: { | ||
restaurant, | ||
}, | ||
}); | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export { createLikeButtonPresenterWithResto }; |
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
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,56 @@ | ||
import FavRestoIdb from '../src/scripts/data/resto-idb'; | ||
import * as TestFactories from './helpers/testFactories'; | ||
|
||
const addLikeButtonContainer = () => { | ||
document.body.innerHTML = '<div id="likeButtonContainer"></div>'; | ||
}; | ||
|
||
describe('Unliking Resto', () => { | ||
beforeEach(async () => { | ||
addLikeButtonContainer(); | ||
await FavRestoIdb.putResto({ id: 1 }); | ||
}); | ||
|
||
afterEach(async () => { | ||
await FavRestoIdb.deleteResto(1); | ||
}); | ||
|
||
it('should display unlike widget when the resto has been liked', async () => { | ||
await TestFactories.createLikeButtonPresenterWithResto({ id: 1 }); | ||
|
||
expect( | ||
document.querySelector('[aria-label="unlike this resto"]'), | ||
).toBeTruthy(); | ||
}); | ||
|
||
it('should not display unlike widget when the resto has been liked', async () => { | ||
await TestFactories.createLikeButtonPresenterWithResto({ id: 1 }); | ||
|
||
expect( | ||
document.querySelector('[aria-label="like this resto"]'), | ||
).toBeFalsy(); | ||
}); | ||
|
||
it('should be able to remove liked resto from the list', async () => { | ||
await TestFactories.createLikeButtonPresenterWithResto({ id: 1 }); | ||
|
||
document | ||
.querySelector('[aria-label="unlike this resto"]') | ||
.dispatchEvent(new Event('click')); | ||
const allResto = await FavRestoIdb.getAllResto(); | ||
|
||
expect(allResto).toEqual([]); | ||
}); | ||
|
||
it('should not throw error if the unliked resto is not in the list', async () => { | ||
await TestFactories.createLikeButtonPresenterWithResto({ id: 1 }); | ||
|
||
await FavRestoIdb.deleteResto(1); | ||
document | ||
.querySelector('[aria-label="unlike this resto"]') | ||
.dispatchEvent(new Event('click')); | ||
const allResto = await FavRestoIdb.getAllResto(); | ||
|
||
expect(allResto).toEqual([]); | ||
}); | ||
}); |
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
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