0

I have a function call ordersConfirmation with an argument Orders[]. It works when passing the list of array from repository.

According to Entity setting, I thought there is no restriction with setting strict: false.
However, I got error when I was building unit test(see below).
The build pass if I change the argument to orders: any[] instead of orders: Orders[] since there is no validation on the object itself but I don't think it is the right way.
Is there a better way or am I doing wrong?

Function:

export function ordersConfirmation(orders: Orders[]) {
  orders.forEach(function (order) {

  })
  return;
}

Model:

@model({settings: {strict: false}})
export class Orders extends Entity {
  @property({
    type: 'string',
    id: true,
    required: true,
  })
  _id: string;

  @property({
    type: 'number',
    required: true,
  })
  status: number;

  [prop: string]: any;

  constructor(data?: Partial<Orders>) {
    super(data);
  }
}

Test case:

  it('ordersConfirmation() failed with error order', () => {
    const expectedError = new HttpErrors.Conflict('Error');
    const orders = [{
      _id: "5d635c090ac9bb4b4b3a86c7",
      status: 2,
    }];
    expect(() => ordersConfirmation(orders)).to.throw(expectedError);
  });

Error Message:

src/__tests__/unit/services.quote.unit.ts:46:37 - error TS2345: Argument of type '{ _id: string; status: number; }[]' is not assignable to parameter of type 'Orders[]'.
  Type '{ _id: string; status: number; }' is missing the following properties from type 'Orders': getId, getIdObject, toJSON, toObject

46     expect(() => ordersConfirmation(orders)).to.throw(expectedError);
                                       ~~~~~~
Roy Pun
  • 493
  • 4
  • 8

1 Answers1

2

This is array of Object

const orders = [{
    _id: "5d635c090ac9bb4b4b3a86c7",
    status: 2,
}];

This is array of Orders

const orders = [
    new Orders({
        _id: "5d635c090ac9bb4b4b3a86c7",
        status: 2,
    })
];

Zhikai Xiong
  • 357
  • 2
  • 9
  • 1
    This solved my issue. However, the reason is not related to the type of array. It is because `Entity` only accept partial object by using constructor to declare the `Orders` from `constructor(data?: Partial) `. thanks! – Roy Pun Aug 28 '19 at 07:06