-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: defend against for/in use on arrays (FE2) (#5662)
* fix: defend against for/in use on arrays * Change files * test: added some basic tests for array observers Co-authored-by: EisenbergEffect <[email protected]>
- Loading branch information
1 parent
d7c903e
commit 963ec19
Showing
4 changed files
with
77 additions
and
17 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@microsoft-fast-element-8e5ad334-ac12-466b-8440-415abd011eb0.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "fix: defend against for/in use on arrays", | ||
"packageName": "@microsoft/fast-element", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/web-components/fast-element/src/observation/array-observer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect } from "chai"; | ||
import { Observable } from "./observable"; | ||
import { enableArrayObservation } from "./array-observer"; | ||
import { SubscriberSet } from "./notifier"; | ||
|
||
describe("The ArrayObserver", () => { | ||
it("can be retrieved through Observable.getNotifier()", () => { | ||
enableArrayObservation(); | ||
const array = []; | ||
const notifier = Observable.getNotifier(array); | ||
expect(notifier).to.be.instanceOf(SubscriberSet); | ||
}); | ||
|
||
it("is the same instance for multiple calls to Observable.getNotifier() on the same array", () => { | ||
enableArrayObservation(); | ||
const array = []; | ||
const notifier = Observable.getNotifier(array); | ||
const notifier2 = Observable.getNotifier(array); | ||
expect(notifier).to.equal(notifier2); | ||
}); | ||
|
||
it("is different for different arrays", () => { | ||
enableArrayObservation(); | ||
const notifier = Observable.getNotifier([]); | ||
const notifier2 = Observable.getNotifier([]); | ||
expect(notifier).to.not.equal(notifier2); | ||
}); | ||
|
||
it("doesn't affect for/in loops on arrays when enabled", () => { | ||
enableArrayObservation(); | ||
|
||
const array = [1, 2, 3]; | ||
const keys: string[] = []; | ||
|
||
for (const key in array) { | ||
keys.push(key); | ||
} | ||
|
||
expect(keys).eql(["0", "1", "2"]); | ||
}); | ||
|
||
it("doesn't affect for/in loops on arrays when the array is observed", () => { | ||
enableArrayObservation(); | ||
|
||
const array = [1, 2, 3]; | ||
const keys: string[] = []; | ||
const notifier = Observable.getNotifier(array); | ||
|
||
for (const key in array) { | ||
keys.push(key); | ||
} | ||
|
||
expect(notifier).to.be.instanceOf(SubscriberSet); | ||
expect(keys).eql(["0", "1", "2"]) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters