|
| 1 | +/* Copyright 2024 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +import {isMouseEventInElement} from './dom'; |
| 17 | + |
| 18 | +describe('dom utils', () => { |
| 19 | + describe('isMouseEventInElement', () => { |
| 20 | + [ |
| 21 | + { |
| 22 | + testDesc: 'click is to the left of element', |
| 23 | + clientX: 99, |
| 24 | + clientY: 150, |
| 25 | + }, |
| 26 | + { |
| 27 | + testDesc: 'click is to the right of element', |
| 28 | + clientX: 201, |
| 29 | + clientY: 150, |
| 30 | + }, |
| 31 | + { |
| 32 | + testDesc: 'click is above element', |
| 33 | + clientX: 150, |
| 34 | + clientY: 99, |
| 35 | + }, |
| 36 | + { |
| 37 | + testDesc: 'click is below element', |
| 38 | + clientX: 150, |
| 39 | + clientY: 201, |
| 40 | + }, |
| 41 | + ].forEach(({testDesc, clientX, clientY}) => { |
| 42 | + it(`returns false when ${testDesc}`, () => { |
| 43 | + const event = new MouseEvent('mouseup', {clientX, clientY}); |
| 44 | + const element = document.createElement('div'); |
| 45 | + spyOn(element, 'getBoundingClientRect').and.returnValue( |
| 46 | + new DOMRect(100, 100, 100, 100) |
| 47 | + ); |
| 48 | + |
| 49 | + const result = isMouseEventInElement(event, element); |
| 50 | + |
| 51 | + expect(result).toBeFalse(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns true when click is within element bounds', () => { |
| 56 | + const event = new MouseEvent('mouseup', {clientX: 150, clientY: 150}); |
| 57 | + const element = document.createElement('div'); |
| 58 | + spyOn(element, 'getBoundingClientRect').and.returnValue( |
| 59 | + new DOMRect(100, 100, 100, 100) |
| 60 | + ); |
| 61 | + |
| 62 | + const result = isMouseEventInElement(event, element); |
| 63 | + |
| 64 | + expect(result).toBeTrue(); |
| 65 | + }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments