Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions components/src/core/injector/core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,4 @@ export default class {

if (has('*', this.watchers)) this.watchers['*'].forEach(call);
}

size() {
return {
height: Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight),
width: Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth),
};
}
}
16 changes: 1 addition & 15 deletions components/src/core/injector/core/Core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,4 @@ describe('Core', () => {
expect(core.watchers['*'][0]).toHaveBeenCalled();
});
});

describe('#size', () => {
it('should return document size in proper format', () => {
jest.spyOn(global.document.documentElement, 'scrollHeight', 'get').mockReturnValue(100)
jest.spyOn(global.document.documentElement, 'scrollWidth', 'get').mockReturnValue(300)
jest.spyOn(global.document.documentElement, 'offsetHeight', 'get').mockReturnValue(200)
jest.spyOn(global.document.documentElement, 'offsetWidth', 'get').mockReturnValue(200)

expect(core.size()).toEqual({
width: 300,
height: 200,
});
});
});
});
});
13 changes: 10 additions & 3 deletions components/src/core/injector/core/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ export default (injector, core, options = {}) => new Promise((resolve) => {
core.state = data;

if (!options?.disableAutoResizing) {
injector.emit('$size', core.size());
setInterval(() => injector.emit('$size', core.size()), 300);
const resizeObserver = new ResizeObserver((entries) => {
const [{ contentRect }] = entries;
injector.emit('$size', { height: contentRect.height, width: contentRect.width });
});

resizeObserver.observe(document.body);
}

resolve();
Expand All @@ -20,7 +24,10 @@ export default (injector, core, options = {}) => new Promise((resolve) => {
window.addEventListener('$injector', ({ detail }) => {
let { type, data } = detail;

if (type === '$size') data = core.size();
if (type === '$size') {
const { height, width } = document.body.getBoundingClientRect();
data = { height, width };
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's nice!!!


injector.emit(type, data);
});
Expand Down
53 changes: 37 additions & 16 deletions components/src/core/injector/core/launcher.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import launch from './launcher';


const resizeObserverCtorSpy = jest.fn();
const resizeObserverObserveSpy = jest.fn();
const resizeObserverEntriesStub = [
{
contentRect: {
height: 400,
width: 800,
},
},
];
const ResizeObserverMock = function ResizeObserverMock(callback) {
resizeObserverCtorSpy(callback);

callback(resizeObserverEntriesStub);

return {
observe: resizeObserverObserveSpy,
};
};
Object.defineProperty(global, 'ResizeObserver', {
writable: true,
value: ResizeObserverMock,
});

describe('$init', () => {
let injector;
let core;
Expand All @@ -9,7 +33,6 @@ describe('$init', () => {
beforeEach(() => {
global.window.addEventListener = jest.fn();
global.window.name = 'XXX';
global.setInterval = jest.fn();
global.crypto = {
getRandomValues: jest.fn(() => ['abc']),
};
Expand Down Expand Up @@ -57,27 +80,22 @@ describe('$init', () => {
it('should emit "$size" event', () => {
handler({}, {});

expect(injector.emit.mock.calls[2]).toEqual(['$size', 'SIZE']);
});

it('should get sizes from $size method', () => {
handler({}, {});

expect(core.size).toHaveBeenCalled();
expect(injector.emit.mock.calls[2]).toEqual(['$size', {
height: 400,
width: 800,
}]);
});

it('should set interval', () => {
it('should create a ResizeObserver instance', () => {
handler({}, {});

expect(setInterval).toHaveBeenCalledWith(expect.any(Function), 300);
expect(resizeObserverCtorSpy).toHaveBeenCalledWith(expect.any(Function));
});

it('should set interval for sizing', () => {
it('should call the resize observer\'s observe method with the document body', () => {
handler({}, {});
injector.emit.mock.calls = [];
setInterval.mock.calls[0][0]();

expect(injector.emit).toHaveBeenCalledWith('$size', 'SIZE');
expect(resizeObserverObserveSpy).toHaveBeenCalledWith(document.body);
});
});

Expand All @@ -104,7 +122,10 @@ describe('$init', () => {
it('should fill size for $size type', () => {
handler({ detail: { type: '$size' } });

expect(injector.emit.mock.calls[2]).toEqual(['$size', 'SIZE']);
expect(injector.emit.mock.calls[2]).toEqual(['$size', {
height: 0,
width: 0,
}]);
});
});

Expand Down Expand Up @@ -170,4 +191,4 @@ describe('$init', () => {
expect(injector.emit).toHaveBeenCalledWith('$mounted');
});
});
});
});
1 change: 1 addition & 0 deletions components/src/widgets/view/widget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export default {
&__content-holder {
position: relative;
grid-area: c;
overflow: scroll;
}

&__content {
Expand Down