2

I am trying to write a unit test with Jest for a service in angular app and I have a question about how to mock a data. As part of my mock data I need to mock Array<App> which has an interface like this

    interface App  {
        Color: string;
        Id: string;
        Size: TileSize;
        Text: string;
        Title: string;
        Url: string;
    }

so I tried to mock the data like this in my test file:
const Apps: Array<App> = [
        {
            'Id': 'Chat',
            'Text': 'Chat',
            'Title': 'Go to your chat',
            'Url': 'https://chat.com'
        },
        {
            'Id': 'Mail',
            'Text': 'Mail',
            'Title': 'Go to your Mail',
            'Url': 'https://mail.com'
        }
    ];

MockConfigParserService.mockImplementation(() => {
        return {
            parse: () => {
                return {
                    workloads
                };
            }
        };
    });

My test fails running the service that I am testing by throwing this error

 Unhandled Promise rejection: workloads.slice is not a function ; Zone: ProxyZone ; Task: Promise.then ; Value: TypeError: workloads.slice is not a function

at this line of code in service code (the service works fine outside of test): return this.appsPromise.then(apps => [...apps]);

how should I change mock data for array of apps so it doesn't throw this error? Thanks a lot in davance

skyboyer
  • 22,209
  • 7
  • 57
  • 64
AlreadyLost
  • 767
  • 2
  • 13
  • 28
  • Can you post your `MockConfigParserService` code? Which method do you want to test? – Lin Du Sep 18 '19 at 16:58
  • What does `workloads` look like? Seems like it is not an array, but the Service call expects an array. – wtho Sep 19 '19 at 16:22

0 Answers0