Skip to content

Commit

Permalink
feat: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Jun 19, 2024
1 parent 97830b2 commit da5122d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
26 changes: 14 additions & 12 deletions packages/spacecat-shared-data-access/src/models/site/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,32 @@ function validateConfiguration(config) {
export const Config = (data = {}) => {
const validConfig = validateConfiguration(data);

const self = { ...validConfig };
self.getSlackConfig = () => self.slack;
self.getSlackMentions = (type) => self?.handlers[type]?.mentions?.slack;
self.getHandlerConfig = (type) => self?.handlers[type];
self.getExcludedURLs = (type) => self?.handlers[type]?.excludedURLs;
const state = { ...validConfig };
const self = { state };
self.getSlackConfig = () => state.slack;
self.getSlackMentions = (type) => state?.handlers[type]?.mentions?.slack;
self.getHandlerConfig = (type) => state?.handlers[type];
self.getExcludedURLs = (type) => state?.handlers[type]?.excludedURLs;

self.updateSlackConfig = (channel, workspace, invitedUserCount) => {
self.slack = {
state.slack = {
channel,
workspace,
invitedUserCount,
};
};

self.updateSlackMentions = (type, mentions) => {
const { handlers } = self;
handlers[type] = handlers[type] || {};
handlers[type].mentions.slack = mentions;
state.handlers = state.handlers || {};
state.handlers[type] = state.handlers[type] || {};
state.handlers[type].mentions = state.handlers[type].mentions || {};
state.handlers[type].mentions.slack = mentions;
};

self.updateExcludeURLs = (type, excludedURLs) => {
const { handlers } = self;
handlers[type] = handlers[type] || {};
handlers[type].excludedURLs = excludedURLs;
state.handlers = state.handlers || {};
state.handlers[type] = state.handlers[type] || {};
state.handlers[type].excludedURLs = excludedURLs;
};

return Object.freeze(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('Configuration Model Tests', () => {
const configuration = createConfiguration(validData);
configuration.disableHandlerForSite('broken-backlinks', { getId: () => 'site2', getOrganizationId: () => 'org7' });
const isEnabled = configuration.isHandlerEnabledForSite('broken-backlinks', { getId: () => 'site2', getOrganizationId: () => 'org7' });
expect(isEnabled).to.be.true;
expect(isEnabled).to.be.false;
});

it('disables a handler type for a site when enabled by default', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Organization Model Tests', () => {
const config = org.getConfig();

expect(config).to.be.an('object');
expect(config.slack).to.be.an('object');
expect(config.getSlackConfig()).to.be.an('object');
});

it('creates an organization with provided config', () => {
Expand All @@ -56,11 +56,12 @@ describe('Organization Model Tests', () => {
};
const org = createOrganization({ ...validData, config: conf });
const config = org.getConfig();

expect(config.slack).to.be.an('object');
expect(config.slack.workspace).to.equal('workspace');
expect(config.slack.channel).to.equal('channel');
expect(config.handlers[404].mentions.slack[0]).to.equal('slackId');
const slack = config.getSlackConfig();
const handler = config.getHandlerConfig(404);
expect(slack).to.be.an('object');
expect(slack.workspace).to.equal('workspace');
expect(slack.channel).to.equal('channel');
expect(handler.mentions.slack[0]).to.equal('slackId');
});
});

Expand Down Expand Up @@ -97,10 +98,12 @@ describe('Organization Model Tests', () => {
};
organization.updateConfig(conf);
const updatedConf = organization.getConfig();
expect(updatedConf.slack).to.be.an('object');
expect(updatedConf.slack.workspace).to.equal('workspace');
expect(updatedConf.slack.channel).to.equal('channel');
expect(updatedConf.handlers[404].mentions.slack[0]).to.equal('slackId');
const updatedSlack = updatedConf.getSlackConfig();
const updateHandlerConfig = updatedConf.getHandlerConfig(404);
expect(updatedSlack).to.be.an('object');
expect(updatedSlack.workspace).to.equal('workspace');
expect(updatedSlack.channel).to.equal('channel');
expect(updateHandlerConfig.mentions.slack[0]).to.equal('slackId');
});

it('throws an error when updating with an empty name', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ describe('Site Model Tests', () => {
};
site.updateConfig(conf);
const updatedConf = site.getConfig();
expect(updatedConf.slack).to.be.an('object');
expect(updatedConf.slack.workspace).to.equal('workspace');
expect(updatedConf.slack.channel).to.equal('channel');
const slack = updatedConf.getSlackConfig();
expect(slack).to.be.an('object');
expect(slack.workspace).to.equal('workspace');
expect(slack.channel).to.equal('channel');
expect(updatedConf.getSlackMentions(404)).to.deep.equal(['slackId']);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ describe('Config Tests', () => {
});
});

describe('Config Methods', () => {
it('correctly updates the Slack configuration', () => {
const config = Config();
config.updateSlackConfig('newChannel', 'newWorkspace', 20);

const slackConfig = config.getSlackConfig();
expect(slackConfig.channel).to.equal('newChannel');
expect(slackConfig.workspace).to.equal('newWorkspace');
expect(slackConfig.invitedUserCount).to.equal(20);
});

it('correctly updates the Slack mentions', () => {
const config = Config();
config.updateSlackMentions('404', ['id1', 'id2']);

const slackMentions = config.getSlackMentions('404');
expect(slackMentions).to.deep.equal(['id1', 'id2']);
});

it('correctly updates the excluded URLs', () => {
const config = Config();
config.updateExcludeURLs('404', ['url1', 'url2']);

const excludedURLs = config.getExcludedURLs('404');
expect(excludedURLs).to.deep.equal(['url1', 'url2']);
});
});

describe('fromDynamoItem Static Method', () => {
it('correctly converts from DynamoDB item', () => {
const dynamoItem = {
Expand All @@ -86,8 +114,9 @@ describe('Config Tests', () => {
};
const config = Config.fromDynamoItem(dynamoItem);
const slackMentions = config.getSlackMentions(404);
expect(config.slack.channel).to.equal('channel1');
expect(config.slack.workspace).to.equal('workspace1');
const slackConfig = config.getSlackConfig();
expect(slackConfig.channel).to.equal('channel1');
expect(slackConfig.workspace).to.equal('workspace1');
expect(slackMentions[0]).to.equal('id1');
});
});
Expand Down

0 comments on commit da5122d

Please sign in to comment.