Skip to content

Commit

Permalink
feat: get project and state
Browse files Browse the repository at this point in the history
  • Loading branch information
rofe committed Nov 24, 2023
1 parent e941529 commit ae9799e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 24 deletions.
54 changes: 54 additions & 0 deletions src/extension/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,57 @@ export async function removeConfig(area, prop) {
export async function clearConfig(area) {
return chrome.storage[area].clear();
}

/**
* Returns an existing project configuration.
* @param {Object|string} project The project settings or handle
* @returns {Promise<Object>} The project configuration
*/
export async function getProject(project) {
let owner;
let repo;
if (typeof project === 'string' && project.includes('/')) {
[owner, repo] = project.split('/');
} else {
({ owner, repo } = project);
}
if (owner && repo) {
const handle = `${owner}/${repo}`;
const projectConfig = await getConfig('sync', handle);
if (projectConfig) {
// if service worker, check session storage for auth token
if (typeof window === 'undefined') {
const auth = await getConfig('session', handle) || {};
return {
...projectConfig,
...auth,
};

Check warning on line 84 in src/extension/utils.js

View check run for this annotation

Codecov / codecov/patch

src/extension/utils.js#L80-L84

Added lines #L80 - L84 were not covered by tests
} else {
return projectConfig;
}
}
}
return undefined;

Check warning on line 90 in src/extension/utils.js

View check run for this annotation

Codecov / codecov/patch

src/extension/utils.js#L90

Added line #L90 was not covered by tests
}

/**
* Assembles a state object from multiple storage types.
* @param {Function} cb The function to call with the state object
* @returns {Promise<void>}
*/
export async function getState(cb) {
if (typeof cb === 'function') {
const display = await getConfig('local', 'hlxSidekickDisplay') || false;
const adminVersion = await getConfig('local', 'hlxSidekickAdminVersion');

const pushDown = await getConfig('sync', 'hlxSidekickPushDown') || false;
const projects = await Promise.all((await getConfig('sync', 'hlxSidekickProjects') || [])
.map((handle) => getProject(handle)));
cb({
display,
adminVersion,
pushDown,
projects,
});
}
}
75 changes: 51 additions & 24 deletions test/wtr/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,69 @@ import {
setConfig,
removeConfig,
clearConfig,
getProject,
getState,
} from '../../src/extension/utils.js';

window.chrome = chromeMock;
const sandbox = sinon.createSandbox();

describe('Test utils', () => {
const sandbox = sinon.createSandbox();

before(async () => {
await setUserAgent('HeadlessChrome');
});

it('dummy', async () => {
expect(true).to.be.true;
afterEach(() => {
sandbox.restore();
});
});

it('getConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'get');
await getConfig('local', 'test');
expect(spy.calledWith('test')).to.be.true;
});
it('getConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'get');
await getConfig('local', 'test');
expect(spy.calledWith('test')).to.be.true;
});

it('setConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'set');
const obj = { foo: 'bar' };
await setConfig('local', obj);
expect(spy.calledWith(obj)).to.be.true;
});
it('setConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'set');
const obj = { foo: 'bar' };
await setConfig('local', obj);
expect(spy.calledWith(obj)).to.be.true;
});

it('removeConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'remove');
await removeConfig('local', 'foo');
expect(spy.calledWith('foo')).to.be.true;
});
it('removeConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'remove');
await removeConfig('local', 'foo');
expect(spy.calledWith('foo')).to.be.true;
});

it('clearConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'clear');
await clearConfig('local');
expect(spy.called).to.be.true;
});

it('clearConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'clear');
await clearConfig('local');
expect(spy.called).to.be.true;
it('getProject', async () => {
const spy = sandbox.spy(window.chrome.storage.sync, 'get');
// get project with handle
let project = await getProject('adobe/blog');
expect(spy.calledWith('adobe/blog')).to.be.true;
expect(project.giturl).to.equal('https://github.com/adobe/blog');
// get project with config object
project = await getProject({ owner: 'adobe', repo: 'blog' });
expect(spy.calledWith('adobe/blog')).to.be.true;
expect(project.giturl).to.equal('https://github.com/adobe/blog');
});

it('getState', async () => {
const spy = sandbox.spy(window.chrome.storage.sync, 'get');
const state = await new Promise((resolve) => {
getState((s) => {
resolve(s);
});
});
expect(spy.calledWith('hlxSidekickProjects')).to.be.true;
expect(typeof state).to.equal('object');
expect(Object.keys(state).length).to.equal(4);
});
});

0 comments on commit ae9799e

Please sign in to comment.