@thumbmarkjs/thumbmarkjs 1.7.3 → 1.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ import { Thumbmark } from './thumbmark';
2
+ import { customComponents } from './factory';
3
+ import { getThumbmark, includeComponent as includeGlobalComponent } from './index';
4
+
5
+ describe('Thumbmark custom components', () => {
6
+ beforeEach(() => {
7
+ for (const key of Object.keys(customComponents)) {
8
+ delete customComponents[key];
9
+ }
10
+ });
11
+
12
+ test('custom components stay scoped to the instance that registered them', async () => {
13
+ const tm1 = new Thumbmark({ logging: false });
14
+ const tm2 = new Thumbmark({ logging: false });
15
+
16
+ tm1.includeComponent('instanceOnly', async () => ({
17
+ value: 'tm1'
18
+ }));
19
+
20
+ const tm1Result = await tm1.get({ include: ['instanceOnly'] });
21
+ const tm2Result = await tm2.get({ include: ['instanceOnly'] });
22
+
23
+ expect(tm1Result.components.instanceOnly).toEqual({ value: 'tm1' });
24
+ expect(tm2Result.components.instanceOnly).toBeUndefined();
25
+ });
26
+
27
+ test('deprecated global includeComponent still registers components for getThumbmark', async () => {
28
+ includeGlobalComponent('legacyGlobal', async () => ({
29
+ value: 'global'
30
+ }));
31
+
32
+ const result = await getThumbmark({
33
+ logging: false,
34
+ include: ['legacyGlobal']
35
+ });
36
+
37
+ expect(result.components.legacyGlobal).toEqual({ value: 'global' });
38
+ });
39
+
40
+ test('getThumbmark accepts an optional custom component registry', async () => {
41
+ const result = await getThumbmark({
42
+ logging: false,
43
+ include: ['directCustom']
44
+ }, {
45
+ directCustom: async () => ({
46
+ value: 'direct'
47
+ })
48
+ });
49
+
50
+ expect(result.components.directCustom).toEqual({ value: 'direct' });
51
+ });
52
+
53
+ test('deprecated global includeComponent still registers components for Thumbmark.get()', async () => {
54
+ includeGlobalComponent('legacyGlobal', async () => ({
55
+ value: 'global'
56
+ }));
57
+
58
+ const tm = new Thumbmark({ logging: false });
59
+ const result = await tm.get({
60
+ include: ['legacyGlobal']
61
+ });
62
+
63
+ expect(result.components.legacyGlobal).toEqual({ value: 'global' });
64
+ });
65
+
66
+ test('instance custom components override deprecated global ones for the same key', async () => {
67
+ includeGlobalComponent('sharedKey', async () => ({
68
+ value: 'global'
69
+ }));
70
+
71
+ const tm = new Thumbmark({ logging: false });
72
+ tm.includeComponent('sharedKey', async () => ({
73
+ value: 'instance'
74
+ }));
75
+
76
+ const globalResult = await getThumbmark({
77
+ logging: false,
78
+ include: ['sharedKey']
79
+ });
80
+ const instanceResult = await tm.get({
81
+ include: ['sharedKey']
82
+ });
83
+
84
+ expect(globalResult.components.sharedKey).toEqual({ value: 'global' });
85
+ expect(instanceResult.components.sharedKey).toEqual({ value: 'instance' });
86
+ });
87
+ });
package/src/thumbmark.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { optionsInterface } from "./options";
2
- import { getThumbmark, includeComponent as globalIncludeComponent, ThumbmarkResponse } from './functions';
2
+ import { getThumbmark, ThumbmarkResponse } from './functions';
3
3
  import { getVersion } from "./utils/version";
4
4
  import { defaultOptions } from "./options";
5
- import { componentInterface } from "./factory";
5
+ import { componentFunctionInterface, componentInterface } from "./factory";
6
6
 
7
7
  /**
8
8
  * A client for generating thumbmarks with a persistent configuration.
@@ -10,6 +10,7 @@ import { componentInterface } from "./factory";
10
10
 
11
11
  export class Thumbmark {
12
12
  private options: optionsInterface;
13
+ private customComponents: Record<string, componentFunctionInterface | null>;
13
14
 
14
15
  /**
15
16
  * Creates a new Thumbmarker client instance.
@@ -17,6 +18,7 @@ export class Thumbmark {
17
18
  */
18
19
  constructor(options?: optionsInterface) {
19
20
  this.options = { ...defaultOptions, ...options };
21
+ this.customComponents = {};
20
22
  }
21
23
 
22
24
  /**
@@ -26,7 +28,7 @@ export class Thumbmark {
26
28
  */
27
29
  public async get(overrideOptions?: optionsInterface): Promise<ThumbmarkResponse> {
28
30
  const finalOptions = { ...this.options, ...overrideOptions };
29
- return getThumbmark(finalOptions);
31
+ return getThumbmark(finalOptions, this.customComponents);
30
32
  }
31
33
  public getVersion(): string {
32
34
  return getVersion()
@@ -37,6 +39,6 @@ export class Thumbmark {
37
39
  * @param fn - The component function
38
40
  */
39
41
  public includeComponent(key: string, fn: (options?: optionsInterface) => Promise<componentInterface | null>) {
40
- globalIncludeComponent(key, fn);
42
+ this.customComponents[key] = fn;
41
43
  }
42
- }
44
+ }