aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/notice-controller-test.js
blob: cf00daeba673005fe3caaa031872fbbb4b9b26d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const assert = require('assert')
const extend = require('xtend')
const rp = require('request-promise')
const nock = require('nock')
const configManagerGen = require('../lib/mock-config-manager')
const NoticeController = require('../../app/scripts/notice-controller')
const STORAGE_KEY = 'metamask-persistance-key'

describe('notice-controller', function() {
  var noticeController

  beforeEach(function() {
    // simple localStorage polyfill
    window.localStorage = {}
    if (window.localStorage.clear) window.localStorage.clear()
    let configManager = configManagerGen()
    noticeController = new NoticeController({
      configManager: configManager,
    })
  })

  describe('notices', function() {
    describe('#getNoticesList', function() {
      it('should return an empty array when new', function() {
        var testList = [{
          id:0,
          read:false,
          title:"Futuristic Notice"
        }]
        var result = noticeController.getNoticesList()
        assert.equal(result.length, 0)
      })
    })

    describe('#setNoticesList', function() {
      it('should set data appropriately', function () {
        var testList = [{
          id:0,
          read:false,
          title:"Futuristic Notice"
        }]
        noticeController.setNoticesList(testList)
        var testListId = noticeController.getNoticesList()[0].id
        assert.equal(testListId, 0)
      })
    })

    describe('#updateNoticeslist', function() {
      it('should integrate the latest changes from the source', function() {
        var testList = [{
          id:55,
          read:false,
          title:"Futuristic Notice"
        }]
        noticeController.setNoticesList(testList)
        noticeController.updateNoticesList().then(() => {
          var newList = noticeController.getNoticesList()
          assert.ok(newList[0].id === 55)
          assert.ok(newList[1])
        })
      })
      it('should not overwrite any existing fields', function () {
        var testList = [{
          id:0,
          read:false,
          title:"Futuristic Notice"
        }]
        noticeController.setNoticesList(testList)
        noticeController.updateNoticesList().then(() => {
          var newList = noticeController.getNoticesList()
          assert.equal(newList[0].id, 0)
          assert.equal(newList[0].title, "Futuristic Notice")
          assert.equal(newList.length, 1)
        })
      })
    })

    describe('#markNoticeRead', function () {
      it('should mark a notice as read', function () {
        var testList = [{
          id:0,
          read:false,
          title:"Futuristic Notice"
        }]
        noticeController.setNoticesList(testList)
        noticeController.markNoticeRead(testList[0])
        var newList = noticeController.getNoticesList()
        assert.ok(newList[0].read)
      })
    })

    describe('#getLatestUnreadNotice', function () {
      it('should retrieve the latest unread notice', function () {
        var testList = [
          {id:0,read:true,title:"Past Notice"},
          {id:1,read:false,title:"Current Notice"},
          {id:2,read:false,title:"Future Notice"},
        ]
        noticeController.setNoticesList(testList)
        var latestUnread = noticeController.getLatestUnreadNotice()
        assert.equal(latestUnread.id, 2)
      })
      it('should return undefined if no unread notices exist.', function () {
        var testList = [
          {id:0,read:true,title:"Past Notice"},
          {id:1,read:true,title:"Current Notice"},
          {id:2,read:true,title:"Future Notice"},
        ]
        noticeController.setNoticesList(testList)
        var latestUnread = noticeController.getLatestUnreadNotice()
        assert.ok(!latestUnread)
      })
    })
  })

})