Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Last active February 23, 2025 02:51
Show Gist options
  • Select an option

  • Save ThomasBurleson/d2d62c8086e562621e597ba330aa38d6 to your computer and use it in GitHub Desktop.

Select an option

Save ThomasBurleson/d2d62c8086e562621e597ba330aa38d6 to your computer and use it in GitHub Desktop.

Revisions

  1. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,9 @@ import { NxModule } from '@nrwl/nx';
    * NgRx feature slice for 'Cars' state
    */
    import { LoadCars, CarsLoaded } from './cars.actions';
    import { CarsState, Entity, initialState, carsReducer } from './cars.reducer';
    import { CarsEffects } from './cars.effects';
    import { CarsFacade } from './cars.facade';
    import { CarsState, Entity, initialState, carsReducer } from './cars.reducer';

    /**
    * The full-app NgRx state only has a 'cars' feature slice
  2. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,6 @@ import { NxModule } from '@nrwl/nx';
    */
    import { LoadCars, CarsLoaded } from './cars.actions';
    import { CarsState, Entity, initialState, carsReducer } from './cars.reducer';

    import { CarsEffects } from './cars.effects';
    import { CarsFacade } from './cars.facade';

  3. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -88,9 +88,10 @@ describe('CarsFacade', () => {
    });

    /**
    * Use `CarsLoaded` to manually submit list for state management
    * Use `CarsLoaded` to manually submit list for state management and
    * test that our observable is properly streaming data changes.
    */
    it('allCars$ should return the loaded list; and loaded flag == true', async (done) => {
    it('allCars$ should return the current list', async (done) => {
    try {
    let list = await readFirst(facade.allCars$);
    let isLoaded = await readFirst(facade.loaded$);
  4. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -98,7 +98,10 @@ describe('CarsFacade', () => {
    expect(list.length).toBe(0);
    expect(isLoaded).toBe(false);

    store.dispatch(new CarsLoaded([ // Add two (2) cars to our NgRx 'cars' state
    // Here we are testing our NgRx actions, reducers, and selectors.
    // Simulate results of a loadAll() and add two (2) cars to our NgRx 'cars' state.

    store.dispatch(new CarsLoaded([
    createCars('AAA'),
    createCars('BBB')
    ]));
  5. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 4 additions and 10 deletions.
    14 changes: 4 additions & 10 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -72,17 +72,13 @@ describe('CarsFacade', () => {

    expect(list.length).toBe(0); // initially empty
    expect(isLoaded).toBe(false); // initially not loaded

    // Note: the default Nx schematic-generated logic in `cars.effects.ts`
    // will simulate a load and return an empty array. Developers
    // must customize with their own logic.

    facade.loadAll();

    facade.loadAll(); // In our case loadAll() always returns an empty array.

    list = await readFirst(facade.allCars$);
    isLoaded = await readFirst(facade.loaded$);

    expect(isLoaded).toBe(true); // data load completed
    expect(isLoaded).toBe(true); // data load completed
    expect(list.length).toBe(0);

    done();
    @@ -102,9 +98,7 @@ describe('CarsFacade', () => {
    expect(list.length).toBe(0);
    expect(isLoaded).toBe(false);

    // Update our 'cars' state with two (2) cars loaded

    store.dispatch(new CarsLoaded([
    store.dispatch(new CarsLoaded([ // Add two (2) cars to our NgRx 'cars' state
    createCars('AAA'),
    createCars('BBB')
    ]));
  6. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -16,9 +16,11 @@ import { CarsState, Entity, initialState, carsReducer } from './cars.reducer';
    import { CarsEffects } from './cars.effects';
    import { CarsFacade } from './cars.facade';


    interface TestSchema {
    'cars' : CarsState
    /**
    * The full-app NgRx state only has a 'cars' feature slice
    */
    interface TestSchema {
    'cars' : CarsState
    }

    describe('CarsFacade', () => {
    @@ -29,7 +31,7 @@ describe('CarsFacade', () => {
    beforeEach(() => {
    createCars = ( id:string, name = '' ): Entity => ({
    id,
    name: name || `name-${id}`
    name: name ? `name-${id}` : id
    });
    });

  7. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -10,16 +10,15 @@ import { NxModule } from '@nrwl/nx';
    /**
    * NgRx feature slice for 'Cars' state
    */
    import { CarsQuery } from './Cars.selectors';
    import { LoadCars, CarsLoaded } from './Cars.actions';
    import { CarsState, Entity, initialState, CarsReducer } from './Cars.reducer';
    import { LoadCars, CarsLoaded } from './cars.actions';
    import { CarsState, Entity, initialState, carsReducer } from './cars.reducer';

    import { CarsEffects } from './Cars.effects';
    import { CarsFacade } from './Cars.facade';
    import { CarsEffects } from './cars.effects';
    import { CarsFacade } from './cars.facade';


    interface TestSchema {
    'Cars' : CarsState
    'cars' : CarsState
    }

    describe('CarsFacade', () => {
    @@ -34,12 +33,12 @@ describe('CarsFacade', () => {
    });
    });

    describe('used in NgModule', () => {
    describe('used in NgModule', async (done) => {

    beforeEach(() => {
    @NgModule({
    imports: [
    StoreModule.forFeature('Cars', CarsReducer, { initialState }),
    StoreModule.forFeature('cars', carsReducer, { initialState }),
    EffectsModule.forFeature([CarsEffects])
    ],
    providers: [CarsFacade]
  8. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -71,16 +71,18 @@ describe('CarsFacade', () => {

    expect(list.length).toBe(0); // initially empty
    expect(isLoaded).toBe(false); // initially not loaded

    // Note: the default Nx schematic-generated logic in `cars.effects.ts`
    // will simulate a load and return an empty array.
    // will simulate a load and return an empty array. Developers
    // must customize with their own logic.

    facade.loadAll();

    list = await readFirst(facade.allCars$);
    isLoaded = await readFirst(facade.loaded$);

    expect(list.length).toBe(0);
    expect(isLoaded).toBe(true);
    expect(isLoaded).toBe(true); // data load completed
    expect(list.length).toBe(0);

    done();
    } catch (err) {
  9. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -99,6 +99,8 @@ describe('CarsFacade', () => {
    expect(list.length).toBe(0);
    expect(isLoaded).toBe(false);

    // Update our 'cars' state with two (2) cars loaded

    store.dispatch(new CarsLoaded([
    createCars('AAA'),
    createCars('BBB')
  10. ThomasBurleson revised this gist Aug 12, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -69,10 +69,12 @@ describe('CarsFacade', () => {
    let list = await readFirst(facade.allCars$);
    let isLoaded = await readFirst(facade.loaded$);

    expect(list.length).toBe(0);
    expect(isLoaded).toBe(false);
    expect(list.length).toBe(0); // initially empty
    expect(isLoaded).toBe(false); // initially not loaded

    facade.loadAll();
    // Note: the default Nx schematic-generated logic in `cars.effects.ts`
    // will simulate a load and return an empty array.
    facade.loadAll();

    list = await readFirst(facade.allCars$);
    isLoaded = await readFirst(facade.loaded$);
  11. ThomasBurleson created this gist Aug 11, 2018.
    118 changes: 118 additions & 0 deletions cars.facade.spec.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    import { NgModule } from '@angular/core';
    import { TestBed } from '@angular/core/testing';
    import { readFirst } from '@nrwl/nx/testing';

    import { EffectsModule } from '@ngrx/effects';
    import { StoreModule, Store } from '@ngrx/store';

    import { NxModule } from '@nrwl/nx';

    /**
    * NgRx feature slice for 'Cars' state
    */
    import { CarsQuery } from './Cars.selectors';
    import { LoadCars, CarsLoaded } from './Cars.actions';
    import { CarsState, Entity, initialState, CarsReducer } from './Cars.reducer';

    import { CarsEffects } from './Cars.effects';
    import { CarsFacade } from './Cars.facade';


    interface TestSchema {
    'Cars' : CarsState
    }

    describe('CarsFacade', () => {
    let facade: CarsFacade;
    let store: Store<TestSchema>;
    let createCars;

    beforeEach(() => {
    createCars = ( id:string, name = '' ): Entity => ({
    id,
    name: name || `name-${id}`
    });
    });

    describe('used in NgModule', () => {

    beforeEach(() => {
    @NgModule({
    imports: [
    StoreModule.forFeature('Cars', CarsReducer, { initialState }),
    EffectsModule.forFeature([CarsEffects])
    ],
    providers: [CarsFacade]
    })
    class CustomFeatureModule {}

    @NgModule({
    imports: [
    NxModule.forRoot(),
    StoreModule.forRoot({}),
    EffectsModule.forRoot([]),
    CustomFeatureModule,
    ]
    })
    class RootModule {}
    TestBed.configureTestingModule({ imports: [RootModule] });

    store = TestBed.get(Store);
    facade = TestBed.get(CarsFacade);
    });

    /**
    * The initially generated facade::loadAll() returns empty array
    */
    it('loadAll() should return empty list with loaded == true', async (done) => {
    try {
    let list = await readFirst(facade.allCars$);
    let isLoaded = await readFirst(facade.loaded$);

    expect(list.length).toBe(0);
    expect(isLoaded).toBe(false);

    facade.loadAll();

    list = await readFirst(facade.allCars$);
    isLoaded = await readFirst(facade.loaded$);

    expect(list.length).toBe(0);
    expect(isLoaded).toBe(true);

    done();
    } catch (err) {
    done.fail(err);
    }
    });

    /**
    * Use `CarsLoaded` to manually submit list for state management
    */
    it('allCars$ should return the loaded list; and loaded flag == true', async (done) => {
    try {
    let list = await readFirst(facade.allCars$);
    let isLoaded = await readFirst(facade.loaded$);

    expect(list.length).toBe(0);
    expect(isLoaded).toBe(false);

    store.dispatch(new CarsLoaded([
    createCars('AAA'),
    createCars('BBB')
    ]));

    list = await readFirst(facade.allCars$);
    isLoaded = await readFirst(facade.loaded$);

    expect(list.length).toBe(2);
    expect(isLoaded).toBe(true);

    done();
    } catch (err) {
    done.fail(err);
    }
    });
    });

    });