mirror of
				https://github.com/mozilla/gecko-dev.git
				synced 2025-11-04 10:18:41 +02:00 
			
		
		
		
	- Updates setState to use `Object.assign` to change the internal state. - Fixes `updateState` to set the ipprotection-panel state property directly, so lit can observe the change. - Adds xpcshell tests for setting the state. Differential Revision: https://phabricator.services.mozilla.com/D255061
		
			
				
	
	
		
			92 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/* Any copyright is dedicated to the Public Domain.
 | 
						|
https://creativecommons.org/publicdomain/zero/1.0/ */
 | 
						|
 | 
						|
"use strict";
 | 
						|
 | 
						|
const { IPProtectionPanel } = ChromeUtils.importESModule(
 | 
						|
  "resource:///modules/ipprotection/IPProtectionPanel.sys.mjs"
 | 
						|
);
 | 
						|
 | 
						|
class FakeIPProtectionPanelElement {
 | 
						|
  constructor() {
 | 
						|
    this.state = {};
 | 
						|
    this.isConnected = false;
 | 
						|
  }
 | 
						|
 | 
						|
  closest() {
 | 
						|
    return {
 | 
						|
      state: "open",
 | 
						|
    };
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Tests that we can set a state and pass it to a fake element.
 | 
						|
 */
 | 
						|
add_task(async function test_setState() {
 | 
						|
  let ipProtectionPanel = new IPProtectionPanel();
 | 
						|
  let fakeElement = new FakeIPProtectionPanelElement();
 | 
						|
  ipProtectionPanel.panel = fakeElement;
 | 
						|
 | 
						|
  ipProtectionPanel.setState({
 | 
						|
    foo: "bar",
 | 
						|
  });
 | 
						|
 | 
						|
  Assert.deepEqual(
 | 
						|
    ipProtectionPanel.state,
 | 
						|
    { foo: "bar" },
 | 
						|
    "The state should be set on the IPProtectionPanel instance"
 | 
						|
  );
 | 
						|
 | 
						|
  Assert.deepEqual(
 | 
						|
    fakeElement.state,
 | 
						|
    {},
 | 
						|
    "The state should not be set on the fake element, as it is not connected"
 | 
						|
  );
 | 
						|
 | 
						|
  fakeElement.isConnected = true;
 | 
						|
 | 
						|
  ipProtectionPanel.setState({
 | 
						|
    isFoo: true,
 | 
						|
  });
 | 
						|
 | 
						|
  Assert.deepEqual(
 | 
						|
    ipProtectionPanel.state,
 | 
						|
    { foo: "bar", isFoo: true },
 | 
						|
    "The state should be set on the IPProtectionPanel instance"
 | 
						|
  );
 | 
						|
 | 
						|
  Assert.deepEqual(
 | 
						|
    fakeElement.state,
 | 
						|
    { foo: "bar", isFoo: true },
 | 
						|
    "The state should be set on the fake element"
 | 
						|
  );
 | 
						|
});
 | 
						|
 | 
						|
/**
 | 
						|
 * Tests that the whole state will be updated when calling updateState directly.
 | 
						|
 */
 | 
						|
add_task(async function test_updateState() {
 | 
						|
  let ipProtectionPanel = new IPProtectionPanel();
 | 
						|
  let fakeElement = new FakeIPProtectionPanelElement();
 | 
						|
  ipProtectionPanel.panel = fakeElement;
 | 
						|
 | 
						|
  ipProtectionPanel.setState({
 | 
						|
    foo: "bar",
 | 
						|
  });
 | 
						|
 | 
						|
  Assert.deepEqual(
 | 
						|
    fakeElement.state,
 | 
						|
    {},
 | 
						|
    "The state should not be set on the fake element, as it is not connected"
 | 
						|
  );
 | 
						|
 | 
						|
  fakeElement.isConnected = true;
 | 
						|
  ipProtectionPanel.updateState();
 | 
						|
 | 
						|
  Assert.deepEqual(
 | 
						|
    fakeElement.state,
 | 
						|
    { foo: "bar" },
 | 
						|
    "The state should be set on the fake element"
 | 
						|
  );
 | 
						|
});
 |