From 7f799526eaaad9df4b01d2cb5d1908f557e4f041 Mon Sep 17 00:00:00 2001 From: Zak Burke Date: Thu, 4 Jan 2024 15:53:20 -0500 Subject: [PATCH] add tests for Stripes, configureLogger Rescue tests from STCOR-674/PR #1268. That work is stale/outdated/incomplete/unscheduled, but the tests are still nice. --- src/Stripes.test.js | 103 +++++++++++++++++++++++ src/configureLogger.test.js | 24 ++++++ test/jest/__mock__/stripesConfig.mock.js | 1 + 3 files changed, 128 insertions(+) create mode 100644 src/Stripes.test.js create mode 100644 src/configureLogger.test.js diff --git a/src/Stripes.test.js b/src/Stripes.test.js new file mode 100644 index 000000000..fdc03c6df --- /dev/null +++ b/src/Stripes.test.js @@ -0,0 +1,103 @@ +import Stripes from './Stripes'; + +describe('Stripes', () => { + describe('clone', () => { + it('returns a matching copy', () => { + const s = new Stripes(); + const c = s.clone(); + expect(s).toEqual(c); + }); + + it('returns a matching copy plus additional properties', () => { + const s = new Stripes(); + const c = s.clone({ monkey: 'bagel' }); + + expect(c).toMatchObject(s); + expect(c).toHaveProperty('monkey', 'bagel'); + }); + }); + + describe('hasInterface', () => { + describe('returns truthy', () => { + it('when the interface is present and the query does not contain a version [boolean, true]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger, discovery: { interfaces: { 'monkey': '3.0' } } }); + expect(s.hasInterface('monkey')).toBe(true); + }); + + it('when the interface is present and compatible with the requested version [string, available interface]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger, discovery: { interfaces: { 'monkey': '3.0' } } }); + expect(s.hasInterface('monkey', '3.0')).toBe('3.0'); + }); + }); + + describe('returns falsy', () => { + it('when the interface is present but incompatible with the requested version [number, 0]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger, discovery: { interfaces: { 'monkey': '3.0' } } }); + expect(s.hasInterface('monkey', '4.0')).toBe(0); + }); + + it('when `discovery` is unavailable [undefined]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger }); + expect(s.hasInterface('monkey')).toBeUndefined(); + }); + + it('when `discovery.interfaces` is unavailable [undefined]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger, discovery: {} }); + expect(s.hasInterface('monkey')).toBeUndefined(); + }); + + it('when the requested interface is absent [undefined]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger, discovery: { interfaces: { 'funky': '3.0' } } }); + expect(s.hasInterface('monkey')).toBeUndefined(); + }); + }); + }); + + describe('hasPerm', () => { + describe('returns true', () => { + it('given hasAllPerms', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger, config: { hasAllPerms: true } }); + expect(s.hasPerm('monkey')).toBe(true); + }); + + it('when requested permissions are assigned', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ + logger, + user: { + perms: { + 'monkey': true, 'bagel': true, 'funky': true, 'chicken': true + } + } + }); + expect(s.hasPerm('monkey,bagel')).toBe(true); + }); + }); + + describe('returns falsy', () => { + it('when requested permissions are not assigned [boolean, false]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ + logger, + user: { + perms: { 'monkey': true, 'bagel': true } + } + }); + expect(s.hasPerm('monkey,funky')).toBe(false); + }); + + it('when user perms are uninitialized [undefined]', () => { + const logger = { log: jest.fn() }; + const s = new Stripes({ logger, user: {} }); + expect(s.hasPerm('monkey')).toBeUndefined(); + }); + }); + }); +}); diff --git a/src/configureLogger.test.js b/src/configureLogger.test.js new file mode 100644 index 000000000..f7c41e222 --- /dev/null +++ b/src/configureLogger.test.js @@ -0,0 +1,24 @@ +import configureLogger from './configureLogger'; + +describe('configureLogger', () => { + it('without an argument, returns a logger with default values', () => { + const logger = configureLogger(); + + expect(logger.categories).toEqual('core,action,xhr'); + expect(logger.prefix).toEqual('stripes'); + expect(logger.timestamp).toBe(false); + }); + + it('with a config argument, returns a logger with custom values', () => { + const config = { + logCategories: 'monkey', + logPrefix: 'bagel', + logTimestamp: true, + }; + const logger = configureLogger(config); + + expect(logger.categories).toEqual(config.logCategories); + expect(logger.prefix).toEqual(config.logPrefix); + expect(logger.timestamp).toBe(config.logTimestamp); + }); +}); diff --git a/test/jest/__mock__/stripesConfig.mock.js b/test/jest/__mock__/stripesConfig.mock.js index 6a1c94df8..c23bea827 100644 --- a/test/jest/__mock__/stripesConfig.mock.js +++ b/test/jest/__mock__/stripesConfig.mock.js @@ -3,6 +3,7 @@ jest.mock('stripes-config', () => ( branding: { style: {}, }, + config: {}, metadata: {}, modules: [], okapi: {