Skip to content

Commit

Permalink
Update popup (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK authored Oct 5, 2024
1 parent 41781fe commit 8ecc2a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
10 changes: 7 additions & 3 deletions src/mjs/popup-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,24 @@ export const updateMenu = async data => {
if (isObjectNotEmpty(data)) {
const { contextInfo: info } = data;
if (info) {
const { canonicalUrl, content, isLink, title, url } = info;
const elm = document.getElementById(LINK_DETAILS);
const nodes = document.querySelectorAll(LINK_MENU);
const { canonicalUrl, content, isLink, selectionText, title, url } = info;
contextInfo.canonicalUrl = canonicalUrl;
contextInfo.content = content;
contextInfo.isLink = !!isLink;
contextInfo.selectionText = selectionText;
contextInfo.title = title;
contextInfo.url = url;
const elm = document.getElementById(LINK_DETAILS);
if (isLink) {
elm.removeAttribute(ATTR_HIDDEN);
document.getElementById(CONTENT_LINK).value = content;
} else {
elm.setAttribute(ATTR_HIDDEN, ATTR_HIDDEN);
}
if (selectionText) {
document.getElementById(CONTENT_PAGE).value = selectionText;
}
const nodes = document.querySelectorAll(LINK_MENU);
for (const node of nodes) {
if (isLink) {
node.removeAttribute(ATTR_DISABLED);
Expand Down
46 changes: 36 additions & 10 deletions test/popup-main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,27 @@ describe('popup-main', () => {
const func = mjs.updateMenu;
beforeEach(() => {
const div = document.createElement('div');
const elm = document.createElement('button');
const elm2 = document.createElement('button');
const elm3 = document.createElement('input');
const div2 = document.createElement('div');
const button = document.createElement('button');
const button2 = document.createElement('button');
const button3 = document.createElement('button');
const button4 = document.createElement('button');
const input = document.createElement('input');
const input2 = document.createElement('input');
const body = document.querySelector('body');
input.id = CONTENT_LINK;
div.id = 'copyLinkDetails';
div.appendChild(elm);
div.appendChild(elm2);
div.setAttribute('hidden', 'hidden');
elm3.id = CONTENT_LINK;
div.appendChild(input);
div.appendChild(button);
div.appendChild(button2);
body.appendChild(div);
body.appendChild(elm3);
input2.id = CONTENT_PAGE;
div2.id = 'copyPageDetails';
div2.appendChild(input2);
div2.appendChild(button3);
div2.appendChild(button4);
body.appendChild(div2);
});

it('should not set attr', async () => {
Expand All @@ -578,7 +588,7 @@ describe('popup-main', () => {

it('should set attr but not value', async () => {
const elm = document.getElementById('copyLinkDetails');
const items = document.querySelectorAll('button');
const items = document.querySelectorAll('#copyLinkDetails button');
const contentLink = document.getElementById(CONTENT_LINK);
const data = {
contextInfo: {
Expand All @@ -599,7 +609,7 @@ describe('popup-main', () => {

it('should set attr but not value', async () => {
const elm = document.getElementById('copyLinkDetails');
const items = document.querySelectorAll('button');
const items = document.querySelectorAll('#copyLinkDetails button');
const contentLink = document.getElementById(CONTENT_LINK);
const data = {
contextInfo: {
Expand All @@ -620,7 +630,7 @@ describe('popup-main', () => {

it('should set attr and value', async () => {
const elm = document.getElementById('copyLinkDetails');
const items = document.querySelectorAll('button');
const items = document.querySelectorAll('#copyLinkDetails button');
const contentLink = document.getElementById(CONTENT_LINK);
const data = {
contextInfo: {
Expand All @@ -641,6 +651,22 @@ describe('popup-main', () => {
}
assert.strictEqual(contentLink.value, 'foo', 'value link');
});

it('should set value', async () => {
const contentPage = document.getElementById(CONTENT_PAGE);
const data = {
contextInfo: {
canonicalUrl: null,
content: '',
isLink: false,
selectionText: 'baz qux',
title: 'bar',
url: 'https://example.com'
}
};
await func(data);
assert.strictEqual(contentPage.value, 'baz qux', 'value pqge');
});
});

describe('prepare tab', () => {
Expand Down

0 comments on commit 8ecc2a1

Please sign in to comment.