|
| 1 | +/******************************************************************************* |
| 2 | + * (c) 2023 unipackage |
| 3 | + * |
| 4 | + * Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0 |
| 5 | + * (the "Apache License"). You may not use this file except in compliance with one of these |
| 6 | + * licenses. You may obtain a copy of the MIT License at |
| 7 | + * |
| 8 | + * https://opensource.org/licenses/MIT |
| 9 | + * |
| 10 | + * Or the Apache License, Version 2.0 at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the MIT License or the Apache License for the specific language governing permissions and |
| 18 | + * limitations under the respective licenses. |
| 19 | + ********************************************************************************/ |
| 20 | + |
| 21 | +import { expect } from "chai" |
| 22 | +import { describe, it } from "mocha" |
| 23 | +import { equal } from "../../src/equal" |
| 24 | + |
| 25 | +// Sample entity class for testing |
| 26 | +class SampleEntity { |
| 27 | + constructor(public id: number, public name: string) {} |
| 28 | +} |
| 29 | + |
| 30 | +describe("equal function", () => { |
| 31 | + it("should return true for equal objects", () => { |
| 32 | + const obj1 = { id: 1, name: "John" } |
| 33 | + const obj2 = { id: 1, name: "John" } |
| 34 | + |
| 35 | + // Using the equal function to compare objects |
| 36 | + const result = equal(obj1, obj2) |
| 37 | + |
| 38 | + expect(result).to.be.true |
| 39 | + }) |
| 40 | + |
| 41 | + it("should return false for non-equal objects", () => { |
| 42 | + const obj1 = { id: 1, name: "John" } |
| 43 | + const obj2 = { id: 2, name: "Jane" } |
| 44 | + |
| 45 | + // Using the equal function to compare objects |
| 46 | + const result = equal(obj1, obj2) |
| 47 | + |
| 48 | + expect(result).to.be.false |
| 49 | + }) |
| 50 | + |
| 51 | + it("should compare specified fields only", () => { |
| 52 | + const entity1 = new SampleEntity(1, "John") |
| 53 | + const entity2 = new SampleEntity(1, "Jane") |
| 54 | + |
| 55 | + // Using the equal function to compare objects with specified fields |
| 56 | + const result = equal(entity1, entity2, ["id"]) |
| 57 | + |
| 58 | + expect(result).to.be.true // Only ID is compared, so it should be considered equal |
| 59 | + }) |
| 60 | +}) |
0 commit comments