-
Notifications
You must be signed in to change notification settings - Fork 13
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 #90 from richard-lopes-ifood/file-storage
File storage: persisting changes over restarts
- Loading branch information
Showing
9 changed files
with
313 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,63 @@ | ||
import { FileStorage } from './storage'; | ||
import { fs as inMemoryFileSystem, vol } from 'memfs'; | ||
|
||
describe('FileStorage', () => { | ||
beforeEach(() => { | ||
vol.reset(); | ||
}); | ||
|
||
it('throws an error when file is invalid', () => { | ||
inMemoryFileSystem.writeFileSync('/.storage', 'abc'); | ||
createFileStorage(); | ||
expect(() => { | ||
createFileStorage().getItem('foo'); | ||
}).toThrow('Invalid file storage content'); | ||
}); | ||
|
||
it('throws an error when path is missing', () => { | ||
expect(() => { | ||
new FileStorage({ enabled: true }); | ||
}).toThrow('FileStorage path option is missing'); | ||
}); | ||
|
||
it('does not initializes store if disabled', () => { | ||
expect(() => { | ||
new FileStorage({ enabled: false, path: '/.storage' }); | ||
inMemoryFileSystem.readFileSync('/.storage', 'utf-8'); | ||
}).toThrowError("ENOENT: no such file or directory, open '/.storage'"); | ||
}); | ||
|
||
it('properly creates an empty storage when the storage file does not exist', () => { | ||
createFileStorage(); | ||
expect(inMemoryFileSystem.readFileSync('/.storage', 'utf-8')).toEqual('{}'); | ||
}); | ||
|
||
it('properly creates a storage with the content of a existing file', () => { | ||
inMemoryFileSystem.writeFileSync( | ||
'/.storage', | ||
'{ "foo": { "key": "value" } }' | ||
); | ||
createFileStorage(); | ||
expect(createFileStorage().getItem('foo')).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('sets and gets item from storage', () => { | ||
expect(() => { | ||
createFileStorage().setItem('foo', { key: 'value' }); | ||
}).not.toThrow(); | ||
|
||
expect(createFileStorage().getItem('foo')).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('gets an unexisting item from storage', () => { | ||
expect(createFileStorage().getItem('foo')).toEqual(undefined); | ||
}); | ||
}); | ||
|
||
function createFileStorage() { | ||
return new FileStorage<'foo'>({ | ||
enabled: true, | ||
path: '/.storage', | ||
fs: inMemoryFileSystem as any, | ||
}); | ||
} |
Oops, something went wrong.