|
| 1 | +import {expect} from 'chai' |
| 2 | +import { |
| 3 | + apply, |
| 4 | + isPolyfilled, |
| 5 | + isSupported, |
| 6 | + union, |
| 7 | + intersection, |
| 8 | + difference, |
| 9 | + symmetricDifference, |
| 10 | + isSubsetOf, |
| 11 | + isSupersetOf, |
| 12 | + isDisjointFrom, |
| 13 | +} from '../src/set-methods.ts' |
| 14 | + |
| 15 | +// eslint-disable-next-line i18n-text/no-en |
| 16 | +describe('Set methods', () => { |
| 17 | + it('has standard isSupported, isPolyfilled, apply API', () => { |
| 18 | + expect(isSupported).to.be.a('function') |
| 19 | + expect(isPolyfilled).to.be.a('function') |
| 20 | + expect(apply).to.be.a('function') |
| 21 | + expect(isSupported()).to.be.a('boolean') |
| 22 | + expect(isPolyfilled()).to.equal(false) |
| 23 | + }) |
| 24 | + |
| 25 | + describe('union', () => { |
| 26 | + it('returns a new Set with elements from both sets', () => { |
| 27 | + const a = new Set([1, 2, 3]) |
| 28 | + const b = new Set([3, 4, 5]) |
| 29 | + const result = union.call(a, b) |
| 30 | + expect(result).to.be.instanceof(Set) |
| 31 | + expect([...result].sort()).to.eql([1, 2, 3, 4, 5]) |
| 32 | + }) |
| 33 | + |
| 34 | + it('handles empty sets', () => { |
| 35 | + const a = new Set([1, 2]) |
| 36 | + const result = union.call(a, new Set()) |
| 37 | + expect([...result].sort()).to.eql([1, 2]) |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('intersection', () => { |
| 42 | + it('returns a new Set with elements in both sets', () => { |
| 43 | + const a = new Set([1, 2, 3]) |
| 44 | + const b = new Set([2, 3, 4]) |
| 45 | + const result = intersection.call(a, b) |
| 46 | + expect(result).to.be.instanceof(Set) |
| 47 | + expect([...result].sort()).to.eql([2, 3]) |
| 48 | + }) |
| 49 | + |
| 50 | + it('returns empty set when no common elements', () => { |
| 51 | + const a = new Set([1, 2]) |
| 52 | + const b = new Set([3, 4]) |
| 53 | + const result = intersection.call(a, b) |
| 54 | + expect(result.size).to.equal(0) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + describe('difference', () => { |
| 59 | + it('returns a new Set with elements in this but not other', () => { |
| 60 | + const a = new Set([1, 2, 3]) |
| 61 | + const b = new Set([2, 3, 4]) |
| 62 | + const result = difference.call(a, b) |
| 63 | + expect(result).to.be.instanceof(Set) |
| 64 | + expect([...result]).to.eql([1]) |
| 65 | + }) |
| 66 | + |
| 67 | + it('returns a copy of this when no overlap', () => { |
| 68 | + const a = new Set([1, 2]) |
| 69 | + const b = new Set([3, 4]) |
| 70 | + const result = difference.call(a, b) |
| 71 | + expect([...result].sort()).to.eql([1, 2]) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('symmetricDifference', () => { |
| 76 | + it('returns elements in one set but not both', () => { |
| 77 | + const a = new Set([1, 2, 3]) |
| 78 | + const b = new Set([2, 3, 4]) |
| 79 | + const result = symmetricDifference.call(a, b) |
| 80 | + expect(result).to.be.instanceof(Set) |
| 81 | + expect([...result].sort()).to.eql([1, 4]) |
| 82 | + }) |
| 83 | + |
| 84 | + it('returns empty set for identical sets', () => { |
| 85 | + const a = new Set([1, 2]) |
| 86 | + const result = symmetricDifference.call(a, new Set([1, 2])) |
| 87 | + expect(result.size).to.equal(0) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + describe('isSubsetOf', () => { |
| 92 | + it('returns true when all elements are in the other set', () => { |
| 93 | + const a = new Set([1, 2]) |
| 94 | + const b = new Set([1, 2, 3]) |
| 95 | + expect(isSubsetOf.call(a, b)).to.equal(true) |
| 96 | + }) |
| 97 | + |
| 98 | + it('returns false when some elements are not in the other set', () => { |
| 99 | + const a = new Set([1, 2, 4]) |
| 100 | + const b = new Set([1, 2, 3]) |
| 101 | + expect(isSubsetOf.call(a, b)).to.equal(false) |
| 102 | + }) |
| 103 | + |
| 104 | + it('returns true for empty set', () => { |
| 105 | + const a = new Set() |
| 106 | + const b = new Set([1, 2]) |
| 107 | + expect(isSubsetOf.call(a, b)).to.equal(true) |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + describe('isSupersetOf', () => { |
| 112 | + it('returns true when this set contains all elements of other', () => { |
| 113 | + const a = new Set([1, 2, 3]) |
| 114 | + const b = new Set([1, 2]) |
| 115 | + expect(isSupersetOf.call(a, b)).to.equal(true) |
| 116 | + }) |
| 117 | + |
| 118 | + it('returns false when other has elements not in this', () => { |
| 119 | + const a = new Set([1, 2]) |
| 120 | + const b = new Set([1, 2, 3]) |
| 121 | + expect(isSupersetOf.call(a, b)).to.equal(false) |
| 122 | + }) |
| 123 | + |
| 124 | + it('returns true when other is empty', () => { |
| 125 | + const a = new Set([1, 2]) |
| 126 | + expect(isSupersetOf.call(a, new Set())).to.equal(true) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + describe('isDisjointFrom', () => { |
| 131 | + it('returns true when sets have no common elements', () => { |
| 132 | + const a = new Set([1, 2]) |
| 133 | + const b = new Set([3, 4]) |
| 134 | + expect(isDisjointFrom.call(a, b)).to.equal(true) |
| 135 | + }) |
| 136 | + |
| 137 | + it('returns false when sets share elements', () => { |
| 138 | + const a = new Set([1, 2, 3]) |
| 139 | + const b = new Set([3, 4]) |
| 140 | + expect(isDisjointFrom.call(a, b)).to.equal(false) |
| 141 | + }) |
| 142 | + |
| 143 | + it('returns true when either set is empty', () => { |
| 144 | + const a = new Set([1, 2]) |
| 145 | + expect(isDisjointFrom.call(a, new Set())).to.equal(true) |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
0 commit comments