feat: return inserted element from insertContent

pull/3586/head
Arman Ozak 6 years ago
parent 2a8f5ebf24
commit 10ac98eafe

@ -6,12 +6,16 @@ import { generateHash } from '../utils';
export class DomInsertionService {
readonly inserted = new Set<number>();
insertContent(contentStrategy: ContentStrategy) {
insertContent<T extends HTMLScriptElement | HTMLStyleElement>(
contentStrategy: ContentStrategy<T>,
): T {
const hash = generateHash(contentStrategy.content);
if (this.inserted.has(hash)) return;
contentStrategy.insertElement();
const element = contentStrategy.insertElement();
this.inserted.add(hash);
return element;
}
}

@ -9,7 +9,7 @@ describe('DomInsertionService', () => {
beforeEach(() => (spectator = createService()));
afterEach(() => styleElements.forEach(element => element.remove()));
afterEach(() => (document.head.innerHTML = ''));
describe('#insertContent', () => {
it('should be able to insert given content', () => {
@ -19,6 +19,11 @@ describe('DomInsertionService', () => {
expect(styleElements[0].textContent).toBe('.test {}');
});
it('should set a hash for the inserted content', () => {
spectator.service.insertContent(CONTENT_STRATEGY.AppendStyleToHead('.test {}'));
expect(spectator.service.inserted.has(1437348290)).toBe(true);
});
it('should insert only once', () => {
expect(spectator.service.inserted.has(1437348290)).toBe(false);
@ -37,9 +42,11 @@ describe('DomInsertionService', () => {
expect(spectator.service.inserted.has(1437348290)).toBe(true);
});
it('should be able to insert given content', () => {
spectator.service.insertContent(CONTENT_STRATEGY.AppendStyleToHead('.test {}'));
expect(spectator.service.inserted.has(1437348290)).toBe(true);
it('should return inserted element', () => {
const element = spectator.service.insertContent(
CONTENT_STRATEGY.AppendStyleToHead('.test {}'),
);
expect(element.tagName).toBe('STYLE');
});
});
});

Loading…
Cancel
Save