This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from 30-seconds/collections-stage-3
Collections stage 3
- Loading branch information
Showing
10 changed files
with
239 additions
and
12 deletions.
There are no files selected for viewing
Submodule configs
updated
from 14f55c to 6b75df
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { InstanceCache } from 'blocks/utilities/instanceCache'; | ||
import { ArgsError } from 'blocks/utilities/error'; | ||
import { convertToSeoSlug } from 'utils'; | ||
|
||
/** | ||
* A collection configuration (i.e. the data and metadata from a content/configs JSON file). | ||
*/ | ||
export class CollectionConfig { | ||
/** | ||
* Create a collection configuration from the JSON data given. | ||
* @param {object} config - Collection configuration data. Must contain: | ||
* - `name` - The name of the configuration. | ||
* - `snippetIds` - Ids of the snippets that make up the collection. | ||
* - `featured` - > 0 if the content is listed, -1 if it's not. | ||
* - `slug` - Base url for the content pages. | ||
* @throws Will throw an error if any of the necessary keys is not present. | ||
*/ | ||
constructor({ | ||
name, | ||
slug, | ||
snippetIds, | ||
featured, | ||
description, | ||
theme = null, | ||
...rest | ||
}) { | ||
if (!name || !slug || !featured || !snippetIds || !snippetIds.length) { | ||
throw new ArgsError( | ||
"Missing required keys. One or more of the following keys were not specified: 'name', 'slug', 'featured', 'snippetIds'" | ||
); | ||
} | ||
|
||
this.name = name; | ||
this.description = description; | ||
this.slug = slug; | ||
this.featured = featured; | ||
this.theme = theme; | ||
this.snippetIds = snippetIds; | ||
Object.keys(rest).forEach(key => { | ||
this[key] = rest[key]; | ||
}); | ||
|
||
CollectionConfig.instances.add(this.id, this); | ||
|
||
return this; | ||
} | ||
|
||
static instances = new InstanceCache(); | ||
|
||
get id() { | ||
return `${this.slug}`; | ||
} | ||
|
||
get icon() { | ||
return this.theme ? this.theme.iconName : null; | ||
} | ||
|
||
get assetPath() { | ||
return `/${global.settings.paths.staticAssetPath}`; | ||
} | ||
|
||
get outPath() { | ||
return global.settings.paths.contentPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { CollectionConfig } from '.'; | ||
import { ArgsError } from 'blocks/utilities/error'; | ||
import { rawCollections } from 'fixtures/blocks/collectionConfigs'; | ||
import { Env } from 'blocks/utilities/env'; | ||
|
||
describe('ColectionConfig', () => { | ||
beforeAll(() => { | ||
Env.setup(); | ||
}); | ||
|
||
describe('constructor', () => { | ||
it('throws an error if called without all required keys', () => { | ||
expect(() => new CollectionConfig({})).toThrow(ArgsError); | ||
}); | ||
}); | ||
|
||
describe('constructed with valid data', () => { | ||
let collectionConfigs = {}; | ||
beforeAll(() => { | ||
collectionConfigs.collection = new CollectionConfig( | ||
rawCollections.collection | ||
); | ||
}); | ||
|
||
it('should contain all passed data', () => { | ||
expect(collectionConfigs.collection.name).toBe( | ||
rawCollections.collection.name | ||
); | ||
expect(collectionConfigs.collection.description).toBe( | ||
rawCollections.collection.description | ||
); | ||
expect(collectionConfigs.collection.slug).toBe( | ||
rawCollections.collection.slug | ||
); | ||
expect(collectionConfigs.collection.featured).toBe( | ||
rawCollections.collection.featured | ||
); | ||
expect(collectionConfigs.collection.snippetIds).toEqual( | ||
rawCollections.collection.snippetIds | ||
); | ||
expect(collectionConfigs.collection.theme).toEqual( | ||
rawCollections.collection.theme | ||
); | ||
}); | ||
|
||
it('should produce the correct id', () => { | ||
expect(collectionConfigs.collection.id).toBe( | ||
rawCollections.collection.slug | ||
); | ||
}); | ||
|
||
it('should produce the correct icon', () => { | ||
expect(collectionConfigs.collection.icon).toBe( | ||
rawCollections.collection.theme.iconName | ||
); | ||
}); | ||
|
||
it('should return the correct asset path', () => { | ||
expect( | ||
collectionConfigs.collection.assetPath.endsWith( | ||
global.settings.paths.staticAssetPath | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
it('should return the correct output path', () => { | ||
expect( | ||
collectionConfigs.collection.outPath.endsWith( | ||
global.settings.paths.contentPath | ||
) | ||
).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.