rodeojeffrey / sequelize-test-helpers-jest

A collection of utilities to help with unit-testing Sequelize models using jest
MIT License
2 stars 2 forks source link

A few more examples, please. #3

Open rconstantine opened 3 years ago

rconstantine commented 3 years ago

Just found this module and I think it may do what I'm looking for.

It looks like the examples are not Jest-specific although this module is for Jest. In particular, I am accustomed to the pattern of a describe block, with a nested set of describe blocks inside, with one or more test calls. A non-sequelize example is this:

describe('Normalize PORT', () => {
    describe('Named PIPE', () => {
        test('should return the named pipe', () => {
            expect(normalizePort('Foo')).toBe('Foo');
        });
    });

    describe('Number PORT', () => {
        test('should return the port number', () => {
            expect(normalizePort(8090)).toBe(8090);
        });
    });
});

I see the example has a describe block but no test calls inside. Instead, this appears to be the first test:

checkModelName(User)('User')

How should checkModelName be used to match the Jest pattern of use?

rconstantine commented 3 years ago

Well, I found that checkModelName can't be put inside a test block. So perhaps the only thing I'm wondering now is if the checkModelName call can be named, like tests can be.

rconstantine commented 3 years ago

I got a couple of things working. The first two tests below work. The third does not, but I think it should. Can you point me in the right direction with the third one? I have many tables with belongsToMany connections using through and foreignKey, so I need to figure this out. When I try the below, a gigantic Object is indicated as received. Any help would be appreciated.

Note: you didn't change the example on the Read.me and I think it is using chai/sinon syntax which doesn't work for Jest.

describe('Check table associations', () => {
    test('AbilityGroups.hasMany(Abilities)', () => {
      const spy = jest.spyOn(AbilityGroups, 'hasMany');
      AbilityGroups.hasMany(Abilities);
      expect(spy).toHaveBeenCalledWith(Abilities);
      spy.mockRestore();
    })
    test('Abilities.belongsTo(AbilityGroups)', () => {
      const spy = jest.spyOn(Abilities, 'belongsTo');
      Abilities.belongsTo(AbilityGroups);
      expect(spy).toHaveBeenCalledWith(AbilityGroups);
      spy.mockRestore();
    });
    test('Users.belongsToMany(Roles)', () => {
      const spy = jest.spyOn(Users, 'belongsToMany');
      Users.belongsToMany(Roles, {through: 'users_roles', foreignKey: 'user_id'});
      expect(spy).toHaveBeenCalledWith(Roles, {through: 'users_roles', foreignKey: 'user_id'});
      spy.mockRestore();
    });
  });

Here is the example from the Read.me:

it("defined a belongsToMany association with Category through CategoriesCompanies as 'categories'", () => {
  expect(Company.belongsToMany).to.have.been.calledWith(Category, {
    through: CategoriesCompanies,
    as: 'categories'
  })
})