简体   繁体   中英

How do I unit test a init() function with Jasmine?

I'm trying to write a unit test for an init function and I'm getting an error where I am calling collectionReport.init() in the test....

TypeError: undefined is not an object

This is the code I am trying to test...

class CollectionsReport {
    constructor({ editCollectionsId, hasCollections}) {

    this.editCollectionsId = editCollectionsId;
    this.hasCollections = hasCollections
}

init({ id, name }) {
    this.id = id;
    this.name = name;

    // need to test this
    if (this.hasCollections) {
        this.collection = this.collections.find(c => c.staticId === 'CAR-COLLECTION');
    }
}

And this is my test so far

describe('CollectionsReport', () => {
    const collectionArgs = {
        editCollectionsId: jasmine.createSpy(),
        hasCollections: false,
    };

    const collections = [
            {
                id: 1,
                name: 'foo',
                staticId: 'CAR-COLLECTIONS',
            },
            {
                id: 2,
                name: 'bar',
                staticId: 'TRUCK-COLLECTIONS',
            },
        ];

    let collectionReport;

    beforeEach(() => {
        collectionReport = new CollectionsReport(collectionArgs);
    });

    describe('.init()', () => {
        it('should test hasCollections', () => {
            collectionReport.init();

            //test this.hasCollections here

        });
    });
});

I'm sure its a mess, so please comment on how to fix and improve it.

Not sure what is the purpose of the CollectionsReport class, but maybe this will lead you to the right direction:

class CollectionsReport {
  constructor({ editCollectionsId, hasCollections}) {
    this.editCollectionsId = editCollectionsId
    this.hasCollections = hasCollections
  }

  init({ collections, staticId }) {
    this.hasCollections = !!collections.find(c => c.staticId === staticId)
  }
}

describe('CollectionsReport', () => {
  const collectionArgs = {
    editCollectionsId: jasmine.createSpy(), // Not really using it
    hasCollections: false
  }

  const collections = [
    {
      id: 1,
      name: 'foo',
      staticId: 'CAR-COLLECTIONS'
    }, {
      id: 2,
      name: 'bar',
      staticId: 'TRUCK-COLLECTIONS'
    }
  ]

  describe('.init()', () => {
    let collectionReport
    beforeEach(() => {
      collectionReport = new CollectionsReport(collectionArgs)
    })

    it('should test hasCollections', () => {
      collectionReport.init({ collections, staticId: 'CAR-COLLECTIONS' })

      expect(collectionReport.hasCollections).toBe(true)
    })

    it('should test hasCollections', () => {
      collectionReport.init({ collections, staticId: 'SOMETHING-ELSE' })

      expect(collectionReport.hasCollections).toBe(false)
    })
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM