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
1 parent
3b1d5c5
commit 8a6a6e1
Showing
12 changed files
with
110 additions
and
96 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
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
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
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
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 |
---|---|---|
@@ -1,64 +1,60 @@ | ||
import * as fs from "fs" | ||
import { when } from "mobx" | ||
import BookStore from './BookStore' | ||
import CartStore from './CartStore' | ||
import {when} from "mobx" | ||
import ShopStore from './ShopStore' | ||
|
||
const bookFetcher = () => Promise.resolve(JSON.parse(fs.readFileSync("./public/books.json"))) | ||
|
||
it('cart store can add new entries', () => { | ||
const bookStore = new BookStore(bookFetcher) | ||
bookStore.updateBooks([{ | ||
const shop = new ShopStore(bookFetcher) | ||
shop.bookStore.updateBooks([{ | ||
id: 1, | ||
price: 3 | ||
}]) | ||
|
||
const cart = new CartStore(bookStore) | ||
cart.addBook(bookStore.books.get(1)) | ||
cart.addBook(bookStore.books.get(1)) | ||
shop.cart.addBook(shop.books.get(1)) | ||
shop.cart.addBook(shop.books.get(1)) | ||
|
||
expect(cart.subTotal).toBe(6) | ||
expect(cart.total).toBe(6) | ||
expect(shop.cart.subTotal).toBe(6) | ||
expect(shop.cart.total).toBe(6) | ||
|
||
cart.entries[0].quantity = 100 | ||
expect(cart.subTotal).toBe(300) | ||
expect(cart.total).toBe(270) | ||
shop.cart.entries[0].quantity = 100 | ||
expect(shop.cart.subTotal).toBe(300) | ||
expect(shop.cart.total).toBe(270) | ||
}) | ||
|
||
it('cart store can clear entries', () => { | ||
const bookStore = new BookStore(bookFetcher) | ||
bookStore.updateBooks([{ | ||
const shop = new ShopStore(bookFetcher) | ||
shop.bookStore.updateBooks([{ | ||
id: 1, | ||
price: 3 | ||
}]) | ||
|
||
const cart = new CartStore(bookStore) | ||
cart.addBook(bookStore.books.get(1)) | ||
shop.cart.addBook(shop.books.get(1)) | ||
|
||
expect(cart.total).toBe(3) | ||
expect(cart.canCheckout).toBe(true) | ||
expect(shop.cart.total).toBe(3) | ||
expect(shop.cart.canCheckout).toBe(true) | ||
|
||
cart.clear() | ||
expect(cart.total).toBe(0) | ||
expect(cart.canCheckout).toBe(false) | ||
shop.cart.clear() | ||
expect(shop.cart.total).toBe(0) | ||
expect(shop.cart.canCheckout).toBe(false) | ||
}) | ||
|
||
it('cart store can clear entries', () => { | ||
const bookStore = new BookStore(bookFetcher) | ||
bookStore.updateBooks([{ | ||
const shop = new ShopStore(bookFetcher) | ||
shop.bookStore.updateBooks([{ | ||
id: 1, | ||
price: 3 | ||
}]) | ||
|
||
const cart = new CartStore(bookStore) | ||
cart.addBook(bookStore.books.get(1)) | ||
shop.cart.addBook(shop.books.get(1)) | ||
|
||
expect(cart.total).toBe(3) | ||
expect(cart.canCheckout).toBe(true) | ||
expect(shop.cart.total).toBe(3) | ||
expect(shop.cart.canCheckout).toBe(true) | ||
|
||
bookStore.updateBooks([]) | ||
expect(cart.total).toBe(3) | ||
expect(cart.canCheckout).toBe(false) | ||
expect(bookStore.books.get(1).isAvailable).toBe(false) | ||
expect(bookStore.books.size).toBe(1) | ||
expect(bookStore.sortedAvailableBooks.length).toBe(0) | ||
shop.bookStore.updateBooks([]) | ||
expect(shop.cart.total).toBe(3) | ||
expect(shop.cart.canCheckout).toBe(false) | ||
expect(shop.books.get(1).isAvailable).toBe(false) | ||
expect(shop.books.size).toBe(1) | ||
expect(shop.sortedAvailableBooks.length).toBe(0) | ||
}) |
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,30 @@ | ||
import BookStore from './BookStore' | ||
import CartStore from './CartStore' | ||
import ViewStore from './ViewStore' | ||
|
||
export default class ShopStore { | ||
fetch | ||
bookStore | ||
cartStore | ||
viewStore | ||
|
||
constructor(fetcher) { | ||
this.fetch = fetcher | ||
this.bookStore = new BookStore(this) | ||
this.cart = new CartStore(this) | ||
this.view = new ViewStore(this) | ||
this.bookStore.loadBooks() | ||
} | ||
|
||
get isLoading() { | ||
return this.bookStore.isLoading | ||
} | ||
|
||
get books() { | ||
return this.bookStore.books | ||
} | ||
|
||
get sortedAvailableBooks() { | ||
return this.bookStore.sortedAvailableBooks | ||
} | ||
} |
Oops, something went wrong.