Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for computed enum values #339

Open
Pmyl opened this issue May 25, 2020 · 2 comments
Open

Support for computed enum values #339

Pmyl opened this issue May 25, 2020 · 2 comments

Comments

@Pmyl
Copy link
Collaborator

Pmyl commented May 25, 2020

Subject of the issue

Right now computed enum values don't work. This bug is not too problematic since it doesn't break the tests.

Your environment

  • ts-auto-mock version: 2.1.22

Steps to reproduce

I found out that enums can have computed values... and we don't support them, we assign 0 to the property if the value is computed.

describe('for enum with computed properties', () => {
  describe('using inline calculations', () => {
    enum Computed1 {
      A = 1 + 5,
      B
    }

    interface IntComputed1 {
      en: Computed1;
    }

    it('should mock the correct values', () => {
      const properties: IntComputed1 = createMock<IntComputed1>();

      expect(properties.en).toEqual(Computed1.A);
    });
  });

  describe('using local function', () => {
    function compute(): number {
      return 4;
    }

    enum Computed2 {
      A = compute(),
      B = 4
    }

    interface IntComputed2 {
      en: Computed2;
    }

    it('should mock the correct values', () => {
      const properties: IntComputed2 = createMock<IntComputed2>();

      expect(properties.en).toEqual(Computed2.A);
    });
  });

  describe('using imported function', () => {
    enum Computed3 {
      A = computed(), // function imported `computed = () => 4`
      B = 4
    }

    interface IntComputed3 {
      en: Computed3;
    }

    it('should mock the correct values', () => {
      const properties: IntComputed3 = createMock<IntComputed3>();

      expect(properties.en).toEqual(Computed3.A);
    });
  });
});

All these tests will fail because the property generated will have 0 as value.

Expected behavior

Generated mocked property have the correct computed value

Actual behavior

Generated mocked property value is 0

@Pmyl Pmyl changed the title Support for computer enum values Support for computed enum values May 25, 2020
@uittorio
Copy link
Member

I love it :) we always learn new things from typescript :)

@Pmyl
Copy link
Collaborator Author

Pmyl commented Sep 13, 2020

Half of this is solved by #526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants