This file contains hidden or 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
| @State<CartStateModel>({ | |
| name: 'cartItems', | |
| defaults: { | |
| cartItems: {}, | |
| }, | |
| }) | |
| export class CartState { | |
| @Action(AddToCart) | |
| addProduct(ctx: StateContext<CartStateModel>, action: AddToCart) { | |
| ctx.setState( |
This file contains hidden or 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
| @Action(LoadCatalog) | |
| loadCatalog(ctx: StateContext<CatalogStateModel>, action: LoadCatalog) { | |
| ctx.setState( | |
| produce(ctx.getState(), draft => { | |
| action.payload.products.forEach(product => { | |
| draft.products[product.sku] = product; | |
| draft.productSkus.push(product.sku); | |
| }); | |
| }), | |
| ); |
This file contains hidden or 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
| ctx.setState( | |
| produce((draft) => { | |
| draft.cartItems[action.payload.sku] = (draft.cartItems[action.payload.sku] || 0) + 1; | |
| }) | |
| ) |
This file contains hidden or 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
| import { TestBed, async } from '@angular/core/testing'; | |
| import { AppComponent } from './app.component'; | |
| describe('AppComponent', () => { | |
| beforeEach(async(() => { | |
| TestBed.configureTestingModule({ | |
| declarations: [ | |
| AppComponent | |
| ], | |
| }).compileComponents(); | |
| })); |
This file contains hidden or 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
| it('should increment and decrement', async () => { | |
| // setup the test via the template syntax | |
| const { getByText, getByTestId, click } = | |
| await createComponent('<counter [counter]="10"></counter>', { | |
| declarations: [CounterComponent], | |
| }); | |
| // or via the component type | |
| const { getByText, getByTestId, click } = | |
| await createComponent( |
This file contains hidden or 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
| async function createComponent(template: string, options: Options): Promise<Result>; | |
| async function createComponent<T>(component: ComponentInput<T>, options: Options): Promise<Result>; |
This file contains hidden or 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
| test('login form submits', async () => { | |
| const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋' }; | |
| const handleLogin = { | |
| emit: jest.fn(), | |
| }; | |
| const { container, getByLabelText, getByText, input, submit } = await createComponent( | |
| { | |
| component: LoginFormComponent, | |
| parameters: { |
This file contains hidden or 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
| const createProduct = ({ | |
| sku = '', | |
| name = '', | |
| image = '', | |
| price = 1, | |
| } = {}): Product => ({ | |
| sku: sku, | |
| name: name || `name-${sku}`, | |
| price, | |
| image: image || `image-${sku}`, |
This file contains hidden or 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
| { | |
| "catalog": { | |
| "products": { | |
| "PRODUCT-AAA": { | |
| "sku": "PRODUCT-AAA", | |
| "name": "name-PRODUCT-AAA", | |
| "price": 1, | |
| "image": "image-PRODUCT-AAA" | |
| }, | |
| "PRODUCT-BBB": { |
This file contains hidden or 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
| export const getCatalogState = createFeatureSelector<fromCatalog.State>('catalog'); | |
| export const getProducts = createSelector( | |
| getCatalogState, | |
| catalog => catalog.products, | |
| ); | |
| export const getProductSkus = createSelector( | |
| getCatalogState, | |
| catalog => catalog.productSkus, | |
| ); |