Skip to content

Commit 74b2836

Browse files
committed
test(calculate-position): add test for smart verticalPosition
1 parent f71c7bd commit 74b2836

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

ember-basic-dropdown/src/components/basic-dropdown.gts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ export default class BasicDropdown extends Component<BasicDropdownSignature> {
282282

283283
@action
284284
reposition(): undefined | RepositionChanges {
285+
debugger
285286
if (!this.publicAPI.isOpen) {
286287
return;
287288
}

ember-basic-dropdown/src/utils/calculate-position.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const getViewData: GetViewData = (trigger, content) => {
6868
content.getBoundingClientRect();
6969
const viewportWidth = document.body.clientWidth || window.innerWidth;
7070
const viewportBottom = scroll.top + window.innerHeight;
71+
debugger
7172
return {
7273
scroll,
7374
// The properties top and left of the trigger client rectangle need to be absolute to

test-app/tests/helpers/index.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
setupTest as upstreamSetupTest,
55
type SetupTestOptions,
66
} from 'ember-qunit';
7+
import { getRootElement } from '@ember/test-helpers';
78

89
// This file exists to provide wrappers around ember-qunit's
910
// test setup functions. This way, you can easily extend the setup that is
@@ -40,4 +41,34 @@ function setupTest(hooks: NestedHooks, options?: SetupTestOptions) {
4041
// Additional setup for unit tests can be done here.
4142
}
4243

43-
export { setupApplicationTest, setupRenderingTest, setupTest };
44+
function setContainerSize(hooks, width, height, overflow) {
45+
let initialWidth: number;
46+
let initialHeight: number;
47+
let initialOverflow: string;
48+
49+
hooks.beforeEach(function() {
50+
const container = getRootElement(); // safer than document.getElementById
51+
52+
console.log(container)
53+
// Store original values
54+
initialWidth = container.style.width;
55+
initialHeight = container.style.height;
56+
initialOverflow = container.style.overflow;
57+
58+
// Set new values
59+
if (width) container.style.width = width;
60+
if (height) container.style.height = height;
61+
if (overflow) container.style.overflow = overflow;
62+
63+
});
64+
65+
hooks.afterEach(function() {
66+
const container = getRootElement();
67+
// Reset to original values to avoid polluting other tests
68+
container.style.width = initialWidth;
69+
container.style.height = initialHeight;
70+
container.style.height = overflow;
71+
});
72+
}
73+
74+
export { setupApplicationTest, setupRenderingTest, setupTest, setContainerSize };

test-app/tests/integration/components/content-test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type TestContext,
1010
} from '@ember/test-helpers';
1111
import type { Dropdown } from 'ember-basic-dropdown/types';
12+
import { setContainerSize } from 'test-app/tests/helpers';
1213

1314
interface ExtendedTestContext extends TestContext {
1415
element: HTMLElement;
@@ -1180,4 +1181,36 @@ module('Integration | Component | basic-dropdown-content', function (hooks) {
11801181
);
11811182
});
11821183
});
1183-
});
1184+
1185+
module('calculate-position', function(hooks) {
1186+
1187+
setContainerSize(hooks, "300px", "200px", "hidden")
1188+
test<ExtendedTestContext>('It properly handles the onMouseLeave action', async function(assert) {
1189+
assert.expect(4);
1190+
1191+
this.set("content", [])
1192+
await render<ExtendedTestContext>(hbs`
1193+
<div style="margin-top: 250px">
1194+
<BasicDropdown @renderInPlace={{true}} as |dd|>
1195+
<dd.Trigger id="trigger">Test</dd.Trigger>
1196+
<dd.Content id="content">
1197+
{{#each this.content as |item|}}
1198+
<p>{{item}}</p>
1199+
{{/each}}
1200+
</dd.Content>
1201+
</BasicDropdown>
1202+
</div>
1203+
`);
1204+
1205+
this.set("content", Array.from(Array(5)).map(i=>"Content"))
1206+
await click('#trigger');
1207+
await triggerEvent(
1208+
getRootNode(this.element).querySelector(
1209+
'#content',
1210+
) as HTMLElement,
1211+
'scroll',
1212+
);
1213+
await this.pauseTest()
1214+
});
1215+
});
1216+
})

0 commit comments

Comments
 (0)