-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added index stored + getIndices to retrieve all indexed classes
- Loading branch information
Showing
9 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { IndexStore } from './index-store'; | ||
|
||
class User {} | ||
|
||
class Twitter {} | ||
|
||
const store = (IndexStore as any).store; | ||
|
||
beforeEach(() => { | ||
// flush internal store | ||
store.length = 0; | ||
}); | ||
|
||
describe('add', () => { | ||
it('stores classes', () => { | ||
IndexStore.add(User); | ||
IndexStore.add(Twitter); | ||
expect(store).toEqual([User, Twitter]); | ||
}); | ||
|
||
it('throw if push a non function', () => { | ||
expect.assertions(1); | ||
try { | ||
IndexStore.add(new User() as any); | ||
} catch (err) { | ||
expect(err).toEqual(new Error('Function expected')); | ||
} | ||
}); | ||
}); | ||
|
||
describe('flush', () => { | ||
beforeEach(() => { | ||
store.push(User, Twitter); | ||
}); | ||
|
||
it('empty internal store', () => { | ||
IndexStore.flush(); | ||
expect(store).toEqual([]); | ||
}); | ||
|
||
it('does not change store reference', () => { | ||
IndexStore.flush(); | ||
expect(store).toBe((IndexStore as any).store); | ||
}); | ||
}); | ||
|
||
describe('getAll', () => { | ||
beforeEach(() => { | ||
store.push(User, Twitter); | ||
}); | ||
|
||
it('returns store content', () => { | ||
expect(IndexStore.getAll()).toEqual([User, Twitter]); | ||
}); | ||
|
||
it('isolate internal array from the one returned', () => { | ||
const copy1 = IndexStore.getAll(); | ||
const copy2 = IndexStore.getAll(); | ||
expect(copy1).toEqual(copy2); | ||
expect(copy1).not.toBe(copy2); | ||
}); | ||
}); |
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,20 @@ | ||
import { AnyClass, IndexedClass } from '../types'; | ||
|
||
export class IndexStore { | ||
private static store: AnyClass[] = []; | ||
|
||
public static add<T>(cls: IndexedClass<T>) { | ||
if (typeof cls !== 'function') { | ||
throw new Error('Function expected'); | ||
} | ||
IndexStore.store.push(cls); | ||
} | ||
|
||
public static flush(): void { | ||
IndexStore.store.length = 0; | ||
} | ||
|
||
public static getAll(): AnyClass[] { | ||
return IndexStore.store.slice(); | ||
} | ||
} |
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