Bug 1797612 - Newtab prevent sponsored topsites from being dragged r=nanj

Differential Revision: https://phabricator.services.mozilla.com/D161000
This commit is contained in:
scott 2022-11-02 22:12:02 +00:00
Родитель fb091ef338
Коммит 624091d13e
3 изменённых файлов: 59 добавлений и 4 удалений

Просмотреть файл

@ -37,7 +37,8 @@ export class TopSiteLink extends React.PureComponent {
*/ */
_allowDrop(e) { _allowDrop(e) {
return ( return (
(this.dragged || !this.props.link.sponsored_position) && (this.dragged ||
(!this.props.link.sponsored_position && !this.props.link.shim)) &&
e.dataTransfer.types.includes("text/topsite-index") e.dataTransfer.types.includes("text/topsite-index")
); );
} }
@ -52,7 +53,7 @@ export class TopSiteLink extends React.PureComponent {
break; break;
case "dragstart": case "dragstart":
event.target.blur(); event.target.blur();
if (this.props.link.sponsored_position) { if (this.props.link.sponsored_position || this.props.link.shim) {
event.preventDefault(); event.preventDefault();
break; break;
} }

Просмотреть файл

@ -11632,7 +11632,7 @@ class TopSiteLink extends (external_React_default()).PureComponent {
_allowDrop(e) { _allowDrop(e) {
return (this.dragged || !this.props.link.sponsored_position) && e.dataTransfer.types.includes("text/topsite-index"); return (this.dragged || !this.props.link.sponsored_position && !this.props.link.shim) && e.dataTransfer.types.includes("text/topsite-index");
} }
onDragEvent(event) { onDragEvent(event) {
@ -11648,7 +11648,7 @@ class TopSiteLink extends (external_React_default()).PureComponent {
case "dragstart": case "dragstart":
event.target.blur(); event.target.blur();
if (this.props.link.sponsored_position) { if (this.props.link.sponsored_position || this.props.link.shim) {
event.preventDefault(); event.preventDefault();
break; break;
} }

Просмотреть файл

@ -469,6 +469,34 @@ describe("<TopSiteLink>", () => {
const wrapper = shallow(<TopSiteLink className="foo bar" />); const wrapper = shallow(<TopSiteLink className="foo bar" />);
assert.ok(wrapper.find("li").hasClass("top-site-outer foo bar")); assert.ok(wrapper.find("li").hasClass("top-site-outer foo bar"));
}); });
describe("#_allowDrop", () => {
let wrapper;
let event;
beforeEach(() => {
event = {
dataTransfer: {
types: ["text/topsite-index"],
},
};
wrapper = shallow(
<TopSiteLink isDraggable={true} onDragEvent={() => {}} />
);
});
it("should be droppable for basic case", () => {
const result = wrapper.instance()._allowDrop(event);
assert.isTrue(result);
});
it("should not be droppable for sponsored_position", () => {
wrapper.setProps({ link: { sponsored_position: 1 } });
const result = wrapper.instance()._allowDrop(event);
assert.isFalse(result);
});
it("should not be droppable for link.shim", () => {
wrapper.setProps({ link: { shim: "foo" } });
const result = wrapper.instance()._allowDrop(event);
assert.isFalse(result);
});
});
describe("#onDragEvent", () => { describe("#onDragEvent", () => {
let simulate; let simulate;
let wrapper; let wrapper;
@ -525,6 +553,32 @@ describe("<TopSiteLink>", () => {
assert.notOk(event.prevented); assert.notOk(event.prevented);
}); });
it("should prevent dragging with sponsored_position from dragstart", () => {
const preventDefault = sinon.stub();
const blur = sinon.stub();
wrapper.setProps({ link: { sponsored_position: 1 } });
wrapper.instance().onDragEvent({
type: "dragstart",
preventDefault,
target: { blur },
});
assert.calledOnce(preventDefault);
assert.calledOnce(blur);
assert.isUndefined(wrapper.instance().dragged);
});
it("should prevent dragging with link.shim from dragstart", () => {
const preventDefault = sinon.stub();
const blur = sinon.stub();
wrapper.setProps({ link: { shim: "foo" } });
wrapper.instance().onDragEvent({
type: "dragstart",
preventDefault,
target: { blur },
});
assert.calledOnce(preventDefault);
assert.calledOnce(blur);
assert.isUndefined(wrapper.instance().dragged);
});
}); });
describe("#generateColor", () => { describe("#generateColor", () => {