Skip to content

Commit

Permalink
update isObjectWith do support partial checking down the object tree f…
Browse files Browse the repository at this point in the history
  • Loading branch information
parisholley committed Aug 26, 2019
1 parent 0721689 commit d410eed
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ node_modules/
.tmp/
sauce.json
.vscode/
.idea
dist/
33 changes: 25 additions & 8 deletions src/Match/MatchObjectWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@ import { Consts } from "../Consts";
import { Utils } from "../Common/Utils";

export class MatchObjectWith<T> implements IMatch {

readonly ___id = Consts.IMATCH_ID_VALUE;

private readonly _value: T;

constructor(value: T) {
this._value = <any>_.cloneDeep(value);
}

___matches(object: Object): boolean {
let match = false;
let partial = _.pick(object, _.keys(this._value));
if (_.isEqual(this._value, partial))
match = true;
return match;
___matches(object: Object, value?: Object): boolean {
const compare = (value || this._value) as any;
const compareKeys = _.keys(compare);
const partial = _.pick(object, compareKeys) as any;
const partialKeys = _.keys(partial);

if (compareKeys.length !== partialKeys.length) {
return false;
}

for (const key of partialKeys) {
const nested = partial[key];
if (_.isArray(nested) || _.isObject(nested)) {
if (!this.___matches(nested, compare[key])) {
return false;
}
} else if (!_.isEqual(nested, compare[key])) {
console.log(nested, '=', compare[key]);
return false;
}
}

return true;
}

toString(): string {
Expand Down
90 changes: 90 additions & 0 deletions test/spec/Mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,96 @@ describe("Mock", () => {
expect(mock.object.doString()).to.eq(undefined);
});

it("should match a method with partial nested object value params", () => {

const bar1nested = new TypeMoqTests.Bar();
bar1nested.anyValue = 42;
bar1nested.enumValue = TypeMoqTests.AnEnum.One;
const bar1 = new TypeMoqTests.Bar();
bar1.value = "Lorem ipsum dolor sit amet";
bar1.nested = bar1nested;

const bar2nested = new TypeMoqTests.Bar();
bar2nested.anyValue = 42;
bar2nested.enumValue = TypeMoqTests.AnEnum.Two;

const bar2 = new TypeMoqTests.Bar();
bar2.value = "Ut enim ad minim veniam";
bar2.nested = bar2nested;
const match = {nested: {enumValue: TypeMoqTests.AnEnum.One}};
const mock = Mock.ofType(TypeMoqTests.Doer);

mock.setup(x => x.doObject(It.isObjectWith(match))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus");

expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus");
expect(mock.object.doObject(bar2)).to.eq(undefined);

bar2nested.enumValue = TypeMoqTests.AnEnum.One;
expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus");

expect(mock.object.doObject(new Object())).to.eq(undefined);
expect(mock.object.doObject({ foo: 'nothing' })).to.eq(undefined);
expect(mock.object.doObject()).to.eq(undefined);
});

it("should match a method with partial nested object array value params", () => {

const bar1nested = new TypeMoqTests.Bar();
bar1nested.anyValue = 42;
bar1nested.enumValue = TypeMoqTests.AnEnum.One;
const bar1 = new TypeMoqTests.Bar();
bar1.value = "Lorem ipsum dolor sit amet";
bar1.nesteds = [bar1nested];

const bar2nested = new TypeMoqTests.Bar();
bar2nested.anyValue = 42;
bar2nested.enumValue = TypeMoqTests.AnEnum.Two;

const bar2 = new TypeMoqTests.Bar();
bar2.value = "Ut enim ad minim veniam";
bar2.nesteds = [bar2nested];
const match = {nesteds: [{enumValue: TypeMoqTests.AnEnum.One}]};
const mock = Mock.ofType(TypeMoqTests.Doer);

mock.setup(x => x.doObject(It.isObjectWith(match))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus");

expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus");
expect(mock.object.doObject(bar2)).to.eq(undefined);

bar2nested.enumValue = TypeMoqTests.AnEnum.One;
expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus");

expect(mock.object.doObject(new Object())).to.eq(undefined);
expect(mock.object.doObject({ foo: 'nothing' })).to.eq(undefined);
expect(mock.object.doObject()).to.eq(undefined);
});

it("should match a method with partial array value params", () => {

const bar1 = new TypeMoqTests.Bar();
bar1.value = "Lorem ipsum dolor sit amet";
bar1.anyValue = 42;
bar1.enumValue = TypeMoqTests.AnEnum.One;
const bar2 = new TypeMoqTests.Bar();
bar2.value = "Ut enim ad minim veniam";
bar2.enumValue = TypeMoqTests.AnEnum.Two;
const match = [{ anyValue: 42, enumValue: TypeMoqTests.AnEnum.One }];
const mock = Mock.ofType(TypeMoqTests.Doer);

mock.setup(x => x.doObject(It.isObjectWith(match))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus");

expect(mock.object.doObject([bar1])).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus");
expect(mock.object.doObject([bar2])).to.eq(undefined);

bar2.anyValue = 42;
bar2.enumValue = TypeMoqTests.AnEnum.One;
expect(mock.object.doObject([bar2])).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus");

expect(mock.object.doObject([new Object()])).to.eq(undefined);
expect(mock.object.doObject([{ foo: 'nothing' }])).to.eq(undefined);
expect(mock.object.doObject([])).to.eq(undefined);
});

it("should match a method with partial object value params", () => {

const bar1 = new TypeMoqTests.Bar();
Expand Down
4 changes: 4 additions & 0 deletions test/spec/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ export module TypeMoqTests {
value: string = '';
anyValue: any = undefined;
enumValue: AnEnum;
nesteds?: Bar[];
nested?: Bar;
}

export interface IBar {
value: string;
anyValue: any;
enumValue: AnEnum;
nested?: IBar;
nesteds?: IBar[];
}

export interface IDo {
Expand Down

0 comments on commit d410eed

Please sign in to comment.