@@ -916,19 +916,9 @@ describe('delete', () => {
916916} )
917917
918918describe ( 'Deploy scope' , ( ) => {
919- test ( 'Returns a deploy-scoped store if the `deployID` parameter is supplied' , async ( ) => {
919+ test ( 'Returns a deploy-scoped store if the `deployID` parameter is supplied and the environment context is present ' , async ( ) => {
920920 const mockToken = 'some-token'
921921 const mockStore = new MockFetch ( )
922- . get ( {
923- headers : { authorization : `Bearer ${ mockToken } ` } ,
924- response : new Response ( value ) ,
925- url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
926- } )
927- . get ( {
928- headers : { authorization : `Bearer ${ mockToken } ` } ,
929- response : new Response ( value ) ,
930- url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
931- } )
932922 . get ( {
933923 headers : { authorization : `Bearer ${ mockToken } ` } ,
934924 response : new Response ( value ) ,
@@ -950,26 +940,52 @@ describe('Deploy scope', () => {
950940
951941 env . NETLIFY_BLOBS_CONTEXT = Buffer . from ( JSON . stringify ( context ) ) . toString ( 'base64' )
952942
953- const siteStore = getStore ( 'images' )
943+ const deployStore = getStore ( { deployID } )
954944
955- const string1 = await siteStore . get ( key )
956- expect ( string1 ) . toBe ( value )
945+ const string = await deployStore . get ( key )
946+ expect ( string ) . toBe ( value )
957947
958- const stream1 = await siteStore . get ( key , { type : 'stream' } )
959- expect ( await streamToString ( stream1 as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
948+ const stream = await deployStore . get ( key , { type : 'stream' } )
949+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
960950
961- const deployStore = getStore ( { deployID } )
951+ expect ( mockStore . fulfilled ) . toBeTruthy ( )
952+ } )
953+
954+ test ( 'Returns a deploy-scoped store if the `deployID` parameter is supplied and the environment context is not present' , async ( ) => {
955+ const mockStore = new MockFetch ( )
956+ . get ( {
957+ headers : { authorization : `Bearer ${ apiToken } ` } ,
958+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
959+ url : `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=deploy:${ deployID } ` ,
960+ } )
961+ . get ( {
962+ response : new Response ( value ) ,
963+ url : signedURL ,
964+ } )
965+ . get ( {
966+ headers : { authorization : `Bearer ${ apiToken } ` } ,
967+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
968+ url : `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=deploy:${ deployID } ` ,
969+ } )
970+ . get ( {
971+ response : new Response ( value ) ,
972+ url : signedURL ,
973+ } )
974+
975+ globalThis . fetch = mockStore . fetch
962976
963- const string2 = await deployStore . get ( key )
964- expect ( string2 ) . toBe ( value )
977+ const deployStore = getStore ( { deployID, siteID, token : apiToken } )
965978
966- const stream2 = await deployStore . get ( key , { type : 'stream' } )
967- expect ( await streamToString ( stream2 as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
979+ const string = await deployStore . get ( key )
980+ expect ( string ) . toBe ( value )
981+
982+ const stream = await deployStore . get ( key , { type : 'stream' } )
983+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
968984
969985 expect ( mockStore . fulfilled ) . toBeTruthy ( )
970986 } )
971987
972- test ( 'Returns a deploy-scoped store if the `getDeployStore` method is called' , async ( ) => {
988+ test ( 'Returns a deploy-scoped store if the `getDeployStore` method is called and the environment context is present ' , async ( ) => {
973989 const mockToken = 'some-token'
974990 const mockStore = new MockFetch ( )
975991 . get ( {
@@ -1004,6 +1020,40 @@ describe('Deploy scope', () => {
10041020
10051021 expect ( mockStore . fulfilled ) . toBeTruthy ( )
10061022 } )
1023+
1024+ test ( 'Returns a deploy-scoped store if the `getDeployStore` method is called and the environment context is not present' , async ( ) => {
1025+ const mockStore = new MockFetch ( )
1026+ . get ( {
1027+ headers : { authorization : `Bearer ${ apiToken } ` } ,
1028+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
1029+ url : `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=deploy:${ deployID } ` ,
1030+ } )
1031+ . get ( {
1032+ response : new Response ( value ) ,
1033+ url : signedURL ,
1034+ } )
1035+ . get ( {
1036+ headers : { authorization : `Bearer ${ apiToken } ` } ,
1037+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
1038+ url : `https://api.netlify.com/api/v1/sites/${ siteID } /blobs/${ key } ?context=deploy:${ deployID } ` ,
1039+ } )
1040+ . get ( {
1041+ response : new Response ( value ) ,
1042+ url : signedURL ,
1043+ } )
1044+
1045+ globalThis . fetch = mockStore . fetch
1046+
1047+ const deployStore = getDeployStore ( { deployID, siteID, token : apiToken } )
1048+
1049+ const string = await deployStore . get ( key )
1050+ expect ( string ) . toBe ( value )
1051+
1052+ const stream = await deployStore . get ( key , { type : 'stream' } )
1053+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
1054+
1055+ expect ( mockStore . fulfilled ) . toBeTruthy ( )
1056+ } )
10071057} )
10081058
10091059describe ( 'Custom `fetch`' , ( ) => {
@@ -1050,4 +1100,24 @@ describe(`getStore`, () => {
10501100 } ) ,
10511101 ) . toThrowError ( MissingBlobsEnvironmentError )
10521102 } )
1103+
1104+ test ( 'Throws when the name of the store is not provided' , async ( ) => {
1105+ const { fetch } = new MockFetch ( )
1106+
1107+ globalThis . fetch = fetch
1108+
1109+ // @ts -expect-error Ignoring types, which expect an argument
1110+ expect ( ( ) => getStore ( ) ) . toThrowError (
1111+ 'The `getStore` method requires the name of the store as a string or as the `name` property of an options object' ,
1112+ )
1113+
1114+ expect ( ( ) =>
1115+ getStore ( {
1116+ token : apiToken ,
1117+ siteID,
1118+ } ) ,
1119+ ) . toThrowError (
1120+ 'The `getStore` method requires the name of the store as a string or as the `name` property of an options object' ,
1121+ )
1122+ } )
10531123} )
0 commit comments